import pathlib
import sqlmodel
import sqlalchemy
import sqlalchemy.engine.base

from .users_loader import users_loader
from .users_collection import users_collection
from .product_app import product_app
from .users_app import users_app
from .directory_image import directory_image
from .reservation_app import reservation_app
from .autoadder import autoadder
from .autoadder_app import autoadder_app

class app_resources:
    def __init__(self, config: object) -> None:
        self.__config = config
        
        self.__database = sqlmodel.create_engine(self.config.database_uri)
        self.__users = users_loader(self.config.users_path).collection

        sqlmodel.SQLModel.metadata.create_all(self.database)

        self.__users_app = users_app(self.users)
        
        self.__directory_image = directory_image(
            self.config.covers_path,
            self.config.thumbnails_dimension
        )

        self.__autoadder = None
        google_api_key = self.config.google_api_key
        google_cx = self.config.google_cx

        if google_api_key is not None and google_cx is not None: 
            self.__autoadder = autoadder(google_api_key, google_cx)
        
        self.__product_app = product_app(
            self.database, 
            self.users,
            self.images
        )

        self.__reservation_app = reservation_app(
            self.database,
            self.users
        )

        self.__autoadder_app = autoadder_app(
            self.users,
            self.autoadder
        )

    @property
    def autoadder(self) -> autoadder:
        return self.__autoadder

    @property
    def autoadder_app(self) -> autoadder_app:
        return self.__autoadder_app

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

    @property
    def reservation_app(self) -> reservation_app:
        return self.__reservation_app

    @property
    def images(self) -> directory_image:
        return self.__directory_image

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

    @property 
    def database(self) -> sqlalchemy.engine.base.Engine:
        return self.__database

    @property
    def users(self) -> users_collection:
        return self.__users
    
    @property
    def config(self) -> object:
        return self.__config

    
