from functools import lru_cache

import decouple
import pydantic
import pathlib
import logging


class BackendSettings(pydantic.BaseSettings):
    """
    Settings of backend
    """

    class Config(pydantic.BaseConfig):
        """
        Configuration of settings file
        """
        case_sensitive: bool = True
        env_file: str = f"/.env"
        validate_assignment: bool = True

    # server configurations
    host: str = decouple.config("HOST", cast=str)
    port: int = decouple.config("PORT", cast=int)

    # database
    connection_string: str = decouple.config("DATABASE.CONNECTION_STRING", cast=str)

    # logging
    logging_dir: str = decouple.config("LOGGING.DIRECTORY", cast=str)

    # Config paths
    shop_config: str = decouple.config("CONFIGURATIONS.SHOPS",cast=str)

    # import configurations
    import_path: str = decouple.config("IMPORT.PATH", cast=str)
    import_files_xml_article_mobi: str = decouple.config("IMPORT.FILES.XML.ARTICLE_Mobi", cast=str)
    import_files_xml_article_detail_mobi: str = decouple.config("IMPORT.FILES.XML.ARTICLE_DETAIL_Mobi", cast=str)
    import_files_xml_article_b2b: str = decouple.config("IMPORT.FILES.XML.ARTICLE_b2b", cast=str)
    import_files_xml_article_detail_b2b: str = decouple.config("IMPORT.FILES.XML.ARTICLE_DETAIL_b2b", cast=str)
    import_files_xml_price_list: str = decouple.config("IMPORT.FILES.XML.PRICE_LIST", cast=str)
    import_files_xml_partner: str = decouple.config("IMPORT.FILES.XML.PARTNER", cast=str)
    import_files_xml_partner_relation: str = decouple.config("IMPORT.FILES.XML.PARTNER_RELATION", cast=str)
    import_files_xml_partner_sales_order_types: str = decouple.config("IMPORT.FILES.XML.PARTNER_SALES_ORDER_TYPE", cast=str)
    import_files_csv_b2b_storage: str = decouple.config("IMPORT.FILES.CSV.STORAGE.B2B", cast=str)
    import_files_csv_pt_open_production: str = decouple.config("IMPORT.FILES.CSV.STORAGE.OPEN_PRODUCTION", cast=str)
    import_files_csv_pt_open_amount: str = decouple.config("IMPORT.FILES.CSV.STORAGE.OPEN_ORDER", cast=str)

    @property
    def xml_import_files(self) -> list[str]:
        """
        Gets all xml import files
        :return: list of xml import files paths
        """
        return [getattr(self, import_file_attr) 
                for import_file_attr in dir(self) if import_file_attr.startswith("import_files_xml")]

    pictures_path: str = decouple.config("PICTURES.PATH", cast=str)

    # mailing
    mailing_server: str = decouple.config("MAILING.SERVER", cast=str)
    mailing_user: str = decouple.config("MAILING.USER", cast=str)
    mailing_user_password: str = decouple.config("MAILING.USER_PASSWORD", cast=str)

    # shop settings
    partner_tool_current_season: str = decouple.config("PARTNER_TOOL.CURRENT_SEASON", cast=str)

    jwt_token: str = decouple.config("JWT.SECRET_KEY", cast=str)
    jwt_subject: str = decouple.config("JWT.SUBJECT", cast=str)
    jwt_algorithm: str = decouple.config("JWT.ALGORITHM", cast=str)
    jwt_refresh_token: str = decouple.config("JWT.REFRESH_TOKEN",cast=str)

@lru_cache()
def get_settings() -> BackendSettings:
    # todo: provide selection of settings by environment
    return BackendSettings()


settings: BackendSettings = get_settings()
