# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

import json


def get_scope_api_map(setup_template_path):
    with open(setup_template_path, 'r') as f:
        return json.load(f)['settings']['authorizer']['scope_api_map']


def scope_api_map_to_endpoints(scope_api_map):
    return {
        k: {
            'method': v.split('/')[1],
            'path': v.replace(f'/{v.split("/")[1]}', ''),
        }
        for k, v in scope_api_map.items()
    }


def configuration(setup_template_path, iapi_endpoints=None, api_endpoints=None, groups=None, users=None):
    endpoints = scope_api_map_to_endpoints(get_scope_api_map(setup_template_path))
    return {
        'endpoints': {
            **endpoints,
            **(iapi_endpoints or {}),
            **(api_endpoints or {})
        },
        # groups and users must be manually created in Auth0 and Application for now
        'groups': groups or {},
        'users': {
            'smoke-organizer@cloudzero.com': {
                'username': 'smoke-organizer@cloudzero.com',
                'groups': [],
            },
            'smoke-member-cz-dev-core@cloudzero.com': {
                'username': 'smoke-member-cz-dev-core@cloudzero.com',
                'groups': [],
            },
            'smoke-superuser@cloudzero.com': {
                'username': 'smoke-superuser@cloudzero.com',
                'groups': [],
            },
            **(users or {})
        }
    }
