import fastapi
import fastapi.staticfiles
import fastapi.responses
import pathlib
import os

from . import *
from .requests import *

class server(fastapi.FastAPI):
    def __init__(self, config_file: str) -> None:
        super().__init__()

        location = pathlib.Path(config_file)
        
        self.__resources = config_loader(app_config) \
        .load(location) \
        .resources
        
        self.__static()
        self.__route_product()
        self.__route_users()

    @property
    def resources(self) -> app_resources:
        return self.__resources

    @property
    def product_app(self) -> product_app:
        return self.__resources.product_app

    @property
    def users_app(self) -> users_app:
        return self.__resources.users_app

    def __static(self) -> None:
        @self.get("/")
        async def root() -> str:
            with pathlib.Path("static/core.html").open() as core:
                return fastapi.responses.HTMLResponse(
                    content = core.read(),
                    status_code = 200
                )
    
        app_directory = fastapi.staticfiles.StaticFiles(
            directory = pathlib.Path("static/")
        )

        covers_directory = fastapi.staticfiles.StaticFiles(
            directory = self.resources.config.covers_path 
        )

        self.mount(
            "/app", 
            app_directory, 
            name = "app_frontend"
        )

        self.mount(
            directory_image.server_path(),
            covers_directory,
            name = "covers"
        )
    
    def __route_product(self) -> None:
        @self.get("/products")
        async def products() -> dict:
            return self.product_app.all()

        @self.get("/product/get/barcode/{barcode}")
        async def product_get_barcode(barcode: int) -> dict:
            return self.product_app.get_barcode(str(barcode))

        @self.get("/product/get/name/{name}")
        async def product_get_name(name: str) -> dict:
            return self.product_app.get_name(name)

        @self.get("/product/search/name/{name}")
        async def product_search_name(name: str) -> dict:
            return self.product_app.search_name(name)

        @self.get("/product/search/author/{author}")
        async def product_search_author(author: str) -> dict:
            return self.product_app.search_author(author)

        @self.get("/product/check/barcode/{barcode}")
        async def product_check_barcode(barcode: int) -> dict:
            return self.product_app.check_barcode(str(barcode))

        @self.get("/product/check/name/{name}")
        async def product_check_name(name: str) -> dict:
            return self.product_app.check_name(name)

        @self.post("/product/update/barcode/{barcode}")
        async def product_barcode_update(
            barcode: str, 
            product: product_update_request
        ) -> dict:
            body = product.dict()
            body["target_barcode"] = barcode

            return self.product_app.update(body)

        @self.post("/product/update/name/{name}")
        async def product_name_update(
            name: str, 
            product: product_update_request
        ) -> dict:
            body = product.dict()
            body["target_name"] = name

            return self.product_app.update(body)

        @self.post("/product/update/image/barcode/{barcode}")
        async def product_image_barcode_update(
            barcode: str,
            product_image: product_update_image_request
        ) -> dict:
            body = product_image.dict()
            body["target_barcode"] = barcode

            return self.product_app.update_image(body)
    
        @self.post("/product/update/image/name/{name}")
        async def product_image_barcode_update(
            name: str,
            product_image: product_update_image_request
        ) -> dict:
            body = product_image.dict()
            body["target_name"] = name

            return self.product_app.update_image(body)

        @self.delete("/product/barcode/{barcode}")
        async def product_barcode_delete(
            barcode: str,
            request: apikey_request
        ) -> dict:
            body = request.dict()
            body["target_barcode"] = barcode

            return self.product_app.delete(body)

        @self.delete("/product/name/{name}")
        async def product_name_delete(
            name: str,
            request: apikey_request
        ) -> dict:
            body = request.dict()
            body["target_name"] = name

            return self.product_app.delete(body)

        @self.post("/product/create")
        async def product_create(product: product_create_request) -> dict:
            return self.product_app.create(product.dict())

    def __route_users(self) -> None:    
        @self.post("/user/login")
        async def user_login(user: user_login_request) -> dict:
            return self.users_app.login(user.nick, user.password)
        
        @self.post("/user")
        async def user_get(body: user_get_request) -> dict:
            return self.users_app.get(body.apikey)
        

instance = server(os.environ["config_file"])
