from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field
from typing import List
import os

class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8', case_sensitive=False, extra='ignore')
    APP_NAME: str = Field(default='Hotel Booking API', description='Application name')
    APP_VERSION: str = Field(default='1.0.0', description='Application version')
    ENVIRONMENT: str = Field(default='development', description='Environment: development, staging, production')
    DEBUG: bool = Field(default=False, description='Debug mode')
    API_V1_PREFIX: str = Field(default='/api/v1', description='API v1 prefix')
    HOST: str = Field(default='0.0.0.0', description='Server host')
    PORT: int = Field(default=8000, description='Server port')
    DB_USER: str = Field(default='root', description='Database user')
    DB_PASS: str = Field(default='', description='Database password')
    DB_NAME: str = Field(default='hotel_db', description='Database name')
    DB_HOST: str = Field(default='localhost', description='Database host')
    DB_PORT: str = Field(default='3306', description='Database port')
    JWT_SECRET: str = Field(default='dev-secret-key-change-in-production-12345', description='JWT secret key')
    JWT_ALGORITHM: str = Field(default='HS256', description='JWT algorithm')
    JWT_ACCESS_TOKEN_EXPIRE_MINUTES: int = Field(default=30, description='JWT access token expiration in minutes')
    JWT_REFRESH_TOKEN_EXPIRE_DAYS: int = Field(default=7, description='JWT refresh token expiration in days')
    CLIENT_URL: str = Field(default='http://localhost:5173', description='Frontend client URL')
    CORS_ORIGINS: List[str] = Field(default_factory=lambda: ['http://localhost:5173', 'http://localhost:3000', 'http://127.0.0.1:5173'], description='Allowed CORS origins')
    RATE_LIMIT_ENABLED: bool = Field(default=True, description='Enable rate limiting')
    RATE_LIMIT_PER_MINUTE: int = Field(default=60, description='Requests per minute per IP')
    LOG_LEVEL: str = Field(default='INFO', description='Log level: DEBUG, INFO, WARNING, ERROR, CRITICAL')
    LOG_FILE: str = Field(default='logs/app.log', description='Log file path')
    LOG_MAX_BYTES: int = Field(default=10485760, description='Max log file size (10MB)')
    LOG_BACKUP_COUNT: int = Field(default=5, description='Number of backup log files')
    SMTP_HOST: str = Field(default='smtp.gmail.com', description='SMTP host')
    SMTP_PORT: int = Field(default=587, description='SMTP port')
    SMTP_USER: str = Field(default='', description='SMTP username')
    SMTP_PASSWORD: str = Field(default='', description='SMTP password')
    SMTP_FROM_EMAIL: str = Field(default='', description='From email address')
    SMTP_FROM_NAME: str = Field(default='Hotel Booking', description='From name')
    UPLOAD_DIR: str = Field(default='uploads', description='Upload directory')
    MAX_UPLOAD_SIZE: int = Field(default=5242880, description='Max upload size in bytes (5MB)')
    ALLOWED_EXTENSIONS: List[str] = Field(default_factory=lambda: ['jpg', 'jpeg', 'png', 'gif', 'webp'], description='Allowed file extensions')
    REDIS_ENABLED: bool = Field(default=False, description='Enable Redis caching')
    REDIS_HOST: str = Field(default='localhost', description='Redis host')
    REDIS_PORT: int = Field(default=6379, description='Redis port')
    REDIS_DB: int = Field(default=0, description='Redis database number')
    REDIS_PASSWORD: str = Field(default='', description='Redis password')
    REQUEST_TIMEOUT: int = Field(default=30, description='Request timeout in seconds')
    HEALTH_CHECK_INTERVAL: int = Field(default=30, description='Health check interval in seconds')
    STRIPE_SECRET_KEY: str = Field(default='', description='Stripe secret key')
    STRIPE_PUBLISHABLE_KEY: str = Field(default='', description='Stripe publishable key')
    STRIPE_WEBHOOK_SECRET: str = Field(default='', description='Stripe webhook secret')
    PAYPAL_CLIENT_ID: str = Field(default='', description='PayPal client ID')
    PAYPAL_CLIENT_SECRET: str = Field(default='', description='PayPal client secret')
    PAYPAL_MODE: str = Field(default='sandbox', description='PayPal mode: sandbox or live')

    @property
    def database_url(self) -> str:
        return f'mysql+pymysql://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}'

    @property
    def is_production(self) -> bool:
        return self.ENVIRONMENT.lower() == 'production'

    @property
    def is_development(self) -> bool:
        return self.ENVIRONMENT.lower() == 'development'

    @property
    def redis_url(self) -> str:
        if self.REDIS_PASSWORD:
            return f'redis://:{self.REDIS_PASSWORD}@{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}'
        return f'redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}'
settings = Settings()