from webargs import fields as webargs_fields, validate as webargs_validate
from webargs.flaskparser import use_kwargs

from dataactbroker.decorators import get_fabs_sub_tier_agencies, requires_login
from dataactbroker.handlers.agency_handler import get_accessible_agencies, get_all_agencies, organize_sub_tier_agencies
from dataactbroker.handlers.help_handler import get_data_sources

from dataactcore.utils.jsonResponse import JsonResponse
from dataactcore.utils.statusCode import StatusCode


# Add the file submission route
def add_domain_routes(app):
    """ Create routes related to domain values for flask app """

    @app.route("/v1/list_agencies/", methods=["GET"])
    @requires_login
    @use_kwargs({
        'perm_level': webargs_fields.String(
            missing='reader',
            validate=webargs_validate.OneOf(('reader', 'writer', 'submitter'))),
        'perm_type': webargs_fields.String(
            missing='mixed',
            validate=webargs_validate.OneOf(('mixed', 'fabs', 'dabs'))),
    })
    def list_agencies(perm_level, perm_type):
        """ Get all agencies the current user has DABS access to. """
        return JsonResponse.create(StatusCode.OK, get_accessible_agencies(perm_level, perm_type))

    @app.route("/v1/list_all_agencies/", methods=["GET"])
    def list_all_agencies():
        """ List all CGAC and FREC Agencies """
        return JsonResponse.create(StatusCode.OK, get_all_agencies())

    @app.route("/v1/list_sub_tier_agencies/", methods=["GET"])
    @get_fabs_sub_tier_agencies
    def list_sub_tier_agencies(sub_tier_agencies):
        """ List all Sub-Tier Agencies user has FABS permissions for
            Args:
            sub_tier_agencies - List of all SubTierAgencies generated by the get_fabs_sub_tier_agencies decorator,
                required to list only sub_tier_agencies that user has FABS permissions for
        """
        return JsonResponse.create(StatusCode.OK, organize_sub_tier_agencies(sub_tier_agencies))

    @app.route("/v1/list_data_sources/", methods=["GET"])
    @requires_login
    def list_data_sources():
        """ Get data sources and the dates they were last updated. """
        return JsonResponse.create(StatusCode.OK, get_data_sources())
