from pydantic_settings import BaseSettings
from pydantic import Field
from typing import Optional, Dict

from dotenv import load_dotenv
load_dotenv(".env")

class SQLiteConfig(BaseSettings):
    driver: str = "sqlite"
    url: Optional[str] = None
    database: str = "database.sqlite"
    prefix: str = ""
    foreign_key_constraints: bool = True
    busy_timeout: Optional[int] = None
    journal_mode: Optional[str] = None
    synchronous: Optional[str] = None


class MySQLConfig(BaseSettings):
    driver: str = "mysql"
    url: Optional[str] = None
    host: str = "127.0.0.1"
    port: int = 3306
    database: str = "laravel"
    username: str = "root"
    password: str = ""
    unix_socket: Optional[str] = ""
    charset: str = "utf8mb4"
    collation: str = "utf8mb4_unicode_ci"
    prefix: str = ""
    prefix_indexes: bool = True
    strict: bool = True
    engine: Optional[str] = None
    ssl_ca: Optional[str] = None


class MariaDBConfig(MySQLConfig):
    driver: str = "mariadb"


class PostgreDB(BaseSettings):
    driver: str = "pgsql"
    url: Optional[str] = None
    host: str = "127.0.0.1"
    port: int = 5432
    database: str = "laravel"
    username: str = "root"
    password: str = ""
    charset: str = "utf8"
    prefix: str = ""
    prefix_indexes: bool = True
    search_path: str = "public"
    sslmode: str = "prefer"

class SQLServerConfig(BaseSettings):
    driver: str = "sqlsrv"
    url: Optional[str] = None
    host: str = "localhost"
    port: int = 1433
    database: str = "laravel"
    username: str = "root"
    password: str = ""
    charset: str = "utf8"
    prefix: str = ""
    prefix_indexes: bool = True
    # encrypt: Optional[str] = "yes"
    # trust_server_certificate: Optional[str] = "false"


class RedisSubConfig(BaseSettings):
    url: Optional[str] = None
    host: str = "127.0.0.1"
    username: Optional[str] = None
    password: Optional[str] = None
    port: int = 6379
    database: int = 0


class RedisOptions(BaseSettings):
    cluster: str = "redis"
    prefix: str = "laravel_database_"
    persistent: bool = False


class RedisConfig(BaseSettings):
    client: str = "phpredis"
    options: RedisOptions = RedisOptions()
    default: RedisSubConfig = RedisSubConfig()
    cache: RedisSubConfig = RedisSubConfig(database=1)


class Settings(BaseSettings):
    connections: Dict[str, BaseSettings] = {
        "global": PostgreDB(
            _env_prefix="DB_",
        ),
    }
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        extra = "allow"
