from pydantic_settings import BaseSettings
from functools import lru_cache
import os


class Settings(BaseSettings):
    # Database - Local SQLite or Remote PostgreSQL
    USE_SQLITE: bool = True  # Set to False for production PostgreSQL
    DB_HOST: str = "192.168.0.201"
    DB_PORT: int = 5432
    DB_NAME: str = "autonet"
    DB_USER: str = "admin"
    DB_PASSWORD: str = ""

    # Redis
    REDIS_HOST: str = "192.168.0.201"
    REDIS_PORT: int = 6379
    REDIS_PASSWORD: str = ""

    # JWT
    SECRET_KEY: str = "your-secret-key-for-dev-123"
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 480  # 8 hours for development

    # Agent
    AGENT_API_KEY: str = ""

    # App
    DEBUG: bool = True

    # Stripe
    STRIPE_SECRET_KEY: str = ""  # sk_test_... or sk_live_...
    STRIPE_PUBLISHABLE_KEY: str = ""  # pk_test_... or pk_live_...
    STRIPE_WEBHOOK_SECRET: str = ""  # whsec_...
    STRIPE_SUCCESS_URL: str = "http://localhost:3000/cc/success"
    STRIPE_CANCEL_URL: str = "http://localhost:3000/cc/purchase"

    # Azure Translator
    AZURE_TRANSLATOR_KEY: str = ""
    AZURE_TRANSLATOR_REGION: str = "koreacentral"

    # Email Settings (SMTP)
    SMTP_HOST: str = "smtp.gmail.com"
    SMTP_PORT: int = 587
    SMTP_USER: str = ""
    SMTP_PASSWORD: str = ""  # App password for Gmail
    SMTP_FROM_EMAIL: str = ""
    SMTP_FROM_NAME: str = "AutonetSellCar"

    # SMS Settings (Twilio)
    TWILIO_ACCOUNT_SID: str = ""
    TWILIO_AUTH_TOKEN: str = ""
    TWILIO_PHONE_NUMBER: str = ""  # Your Twilio phone number

    # Verification Settings
    VERIFICATION_CODE_EXPIRE_MINUTES: int = 10
    EMAIL_VERIFICATION_REQUIRED: bool = True  # Require email verification for signup
    PHONE_VERIFICATION_REQUIRED_FOR_CC: bool = True  # Require phone for CC charging

    @property
    def DATABASE_URL(self) -> str:
        if self.USE_SQLITE:
            # Get the backend directory path
            base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            db_path = os.path.join(base_dir, "autonet.db")
            return f"sqlite:///{db_path}"
        return f"postgresql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"

    @property
    def REDIS_URL(self) -> str:
        return f"redis://:{self.REDIS_PASSWORD}@{self.REDIS_HOST}:{self.REDIS_PORT}/0"

    class Config:
        env_file = ".env"


@lru_cache()
def get_settings() -> Settings:
    """Get cached settings - updated with SMTP credentials"""
    return Settings()
