from osbot_fast_api_serverless.fast_api.routes.Routes__Info import Routes__Info

import sgraph_ai_app_send__ui__admin
from osbot_fast_api.api.decorators.route_path                                       import route_path
from osbot_fast_api.api.routes.Routes__Set_Cookie                                   import Routes__Set_Cookie
from osbot_fast_api_serverless.fast_api.Serverless__Fast_API                        import Serverless__Fast_API
from starlette.responses                                                            import RedirectResponse
from starlette.staticfiles                                                          import StaticFiles
from sgraph_ai_app_send.lambda__admin.admin__config                                 import APP_SEND__UI__ADMIN__ROUTE__PATH__CONSOLE, APP__SEND__ADMIN__FAST_API__TITLE, APP__SEND__ADMIN__FAST_API__DESCRIPTION, APP_SEND__UI__ADMIN__MAJOR__VERSION, APP_SEND__UI__ADMIN__LATEST__VERSION, APP_SEND__UI__ADMIN__START_PAGE
from sgraph_ai_app_send.utils.Version                                               import version__sgraph_ai_app_send

ROUTES_PATHS__APP_SEND__STATIC__ADMIN  = [f'/{APP_SEND__UI__ADMIN__ROUTE__PATH__CONSOLE}']

class Fast_API__SGraph__App__Send__Admin(Serverless__Fast_API):

    def setup(self):
        with self.config as _:
            _.name           = APP__SEND__ADMIN__FAST_API__TITLE
            _.version        = version__sgraph_ai_app_send
            _.description    = APP__SEND__ADMIN__FAST_API__DESCRIPTION
        return super().setup()


    def setup_routes(self):
        self.setup_static_routes()
        self.add_routes(Routes__Info             )
        self.add_routes(Routes__Set_Cookie       )


    # todo: refactor to separate class (focused on setting up this static route)
    #       also these values should all be defined in a Type_Safe class
    def setup_static_routes(self):


        path_static_folder  = sgraph_ai_app_send__ui__admin.path
        path_static         = f"/{APP_SEND__UI__ADMIN__ROUTE__PATH__CONSOLE}"
        path_name           = APP_SEND__UI__ADMIN__ROUTE__PATH__CONSOLE
        major_version       = APP_SEND__UI__ADMIN__MAJOR__VERSION
        latest_version      = APP_SEND__UI__ADMIN__LATEST__VERSION
        start_page          = APP_SEND__UI__ADMIN__START_PAGE
        path_latest_version = f"/{path_name}/{major_version}/{latest_version}/{start_page}.html"
        self.app().mount(path_static, StaticFiles(directory=path_static_folder), name=path_name)

        @route_path(path=f'/{APP_SEND__UI__ADMIN__ROUTE__PATH__CONSOLE}')
        def redirect_to_latest():
            return RedirectResponse(url=path_latest_version)

        self.add_route_get(redirect_to_latest)