# ================================================================
# TENANT SERVICE CONFIGURATION
# services/tenant/app/core/config.py
# ================================================================

"""
Tenant service configuration
Multi-tenant management and subscription handling
"""

from shared.config.base import BaseServiceSettings
import os
from typing import Dict, Tuple, ClassVar

class TenantSettings(BaseServiceSettings):
    """Tenant service specific settings"""
    
    # Service Identity
    APP_NAME: str = "Tenant Service"
    SERVICE_NAME: str = "tenant-service"
    DESCRIPTION: str = "Multi-tenant management and subscription service"
    
    # Database configuration (secure approach - build from components)
    @property
    def DATABASE_URL(self) -> str:
        """Build database URL from secure components"""
        # Try complete URL first (for backward compatibility)
        complete_url = os.getenv("TENANT_DATABASE_URL")
        if complete_url:
            return complete_url

        # Build from components (secure approach)
        user = os.getenv("TENANT_DB_USER", "tenant_user")
        password = os.getenv("TENANT_DB_PASSWORD", "tenant_pass123")
        host = os.getenv("TENANT_DB_HOST", "localhost")
        port = os.getenv("TENANT_DB_PORT", "5432")
        name = os.getenv("TENANT_DB_NAME", "tenant_db")

        return f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{name}"
    
    # Redis Database (dedicated for tenant data)
    REDIS_DB: int = 4

    # Service URLs for usage tracking
    RECIPES_SERVICE_URL: str = os.getenv("RECIPES_SERVICE_URL", "http://recipes-service:8004")
    SUPPLIERS_SERVICE_URL: str = os.getenv("SUPPLIERS_SERVICE_URL", "http://suppliers-service:8005")

    # Subscription Plans
    DEFAULT_PLAN: str = os.getenv("DEFAULT_PLAN", "basic")
    TRIAL_PERIOD_DAYS: int = int(os.getenv("TRIAL_PERIOD_DAYS", "0"))
    
    # Plan Limits
    BASIC_PLAN_LOCATIONS: int = int(os.getenv("BASIC_PLAN_LOCATIONS", "1"))
    BASIC_PLAN_PREDICTIONS_PER_DAY: int = int(os.getenv("BASIC_PLAN_PREDICTIONS_PER_DAY", "100"))
    BASIC_PLAN_DATA_RETENTION_DAYS: int = int(os.getenv("BASIC_PLAN_DATA_RETENTION_DAYS", "90"))
    
    PREMIUM_PLAN_LOCATIONS: int = int(os.getenv("PREMIUM_PLAN_LOCATIONS", "5"))
    PREMIUM_PLAN_PREDICTIONS_PER_DAY: int = int(os.getenv("PREMIUM_PLAN_PREDICTIONS_PER_DAY", "1000"))
    PREMIUM_PLAN_DATA_RETENTION_DAYS: int = int(os.getenv("PREMIUM_PLAN_DATA_RETENTION_DAYS", "365"))
    
    ENTERPRISE_PLAN_LOCATIONS: int = int(os.getenv("ENTERPRISE_PLAN_LOCATIONS", "50"))
    ENTERPRISE_PLAN_PREDICTIONS_PER_DAY: int = int(os.getenv("ENTERPRISE_PLAN_PREDICTIONS_PER_DAY", "10000"))
    ENTERPRISE_PLAN_DATA_RETENTION_DAYS: int = int(os.getenv("ENTERPRISE_PLAN_DATA_RETENTION_DAYS", "1095"))
    
    # Billing Configuration
    BILLING_ENABLED: bool = os.getenv("BILLING_ENABLED", "false").lower() == "true"
    BILLING_CURRENCY: str = os.getenv("BILLING_CURRENCY", "EUR")
    BILLING_CYCLE_DAYS: int = int(os.getenv("BILLING_CYCLE_DAYS", "30"))
    
    # Stripe Proration Configuration
    DEFAULT_PRORATION_BEHAVIOR: str = os.getenv("DEFAULT_PRORATION_BEHAVIOR", "create_prorations")
    UPGRADE_PRORATION_BEHAVIOR: str = os.getenv("UPGRADE_PRORATION_BEHAVIOR", "create_prorations")
    DOWNGRADE_PRORATION_BEHAVIOR: str = os.getenv("DOWNGRADE_PRORATION_BEHAVIOR", "none")
    BILLING_CYCLE_CHANGE_PRORATION: str = os.getenv("BILLING_CYCLE_CHANGE_PRORATION", "create_prorations")
    
    # Stripe Subscription Update Settings
    STRIPE_BILLING_CYCLE_ANCHOR: str = os.getenv("STRIPE_BILLING_CYCLE_ANCHOR", "unchanged")
    STRIPE_PAYMENT_BEHAVIOR: str = os.getenv("STRIPE_PAYMENT_BEHAVIOR", "error_if_incomplete")
    ALLOW_IMMEDIATE_SUBSCRIPTION_CHANGES: bool = os.getenv("ALLOW_IMMEDIATE_SUBSCRIPTION_CHANGES", "true").lower() == "true"
    
    # Resource Limits
    MAX_API_CALLS_PER_MINUTE: int = int(os.getenv("MAX_API_CALLS_PER_MINUTE", "100"))
    MAX_STORAGE_MB: int = int(os.getenv("MAX_STORAGE_MB", "1024"))
    MAX_CONCURRENT_REQUESTS: int = int(os.getenv("MAX_CONCURRENT_REQUESTS", "10"))
    
    # Spanish Business Configuration
    SPANISH_TAX_RATE: float = float(os.getenv("SPANISH_TAX_RATE", "0.21"))  # IVA 21%
    INVOICE_LANGUAGE: str = os.getenv("INVOICE_LANGUAGE", "es")
    SUPPORT_EMAIL: str = os.getenv("SUPPORT_EMAIL", "soporte@bakeryforecast.es")
    
    # Onboarding
    ONBOARDING_ENABLED: bool = os.getenv("ONBOARDING_ENABLED", "true").lower() == "true"
    DEMO_DATA_ENABLED: bool = os.getenv("DEMO_DATA_ENABLED", "true").lower() == "true"
    
    # Compliance
    GDPR_COMPLIANCE_ENABLED: bool = True
    DATA_EXPORT_ENABLED: bool = True
    DATA_DELETION_ENABLED: bool = True
    
    # Stripe Payment Configuration
    STRIPE_PUBLISHABLE_KEY: str = os.getenv("STRIPE_PUBLISHABLE_KEY", "")
    STRIPE_SECRET_KEY: str = os.getenv("STRIPE_SECRET_KEY", "")
    STRIPE_WEBHOOK_SECRET: str = os.getenv("STRIPE_WEBHOOK_SECRET", "")
    
    # Stripe Price IDs for subscription plans
    STARTER_MONTHLY_PRICE_ID: str = os.getenv("STARTER_MONTHLY_PRICE_ID", "price_1Sp0p3IzCdnBmAVT2Gs7z5np")
    STARTER_YEARLY_PRICE_ID: str = os.getenv("STARTER_YEARLY_PRICE_ID", "price_1Sp0twIzCdnBmAVTD1lNLedx")
    PROFESSIONAL_MONTHLY_PRICE_ID: str = os.getenv("PROFESSIONAL_MONTHLY_PRICE_ID", "price_1Sp0w7IzCdnBmAVTp0Jxhh1u")
    PROFESSIONAL_YEARLY_PRICE_ID: str = os.getenv("PROFESSIONAL_YEARLY_PRICE_ID", "price_1Sp0yAIzCdnBmAVTLoGl4QCb")
    ENTERPRISE_MONTHLY_PRICE_ID: str = os.getenv("ENTERPRISE_MONTHLY_PRICE_ID", "price_1Sp0zAIzCdnBmAVTXpApF7YO")
    ENTERPRISE_YEARLY_PRICE_ID: str = os.getenv("ENTERPRISE_YEARLY_PRICE_ID", "price_1Sp15mIzCdnBmAVTuxffMpV5")
    
    # Price ID mapping for easy lookup
    STRIPE_PRICE_ID_MAPPING: ClassVar[Dict[Tuple[str, str], str]] = {
        ('starter', 'monthly'): STARTER_MONTHLY_PRICE_ID,
        ('starter', 'yearly'): STARTER_YEARLY_PRICE_ID,
        ('professional', 'monthly'): PROFESSIONAL_MONTHLY_PRICE_ID,
        ('professional', 'yearly'): PROFESSIONAL_YEARLY_PRICE_ID,
        ('enterprise', 'monthly'): ENTERPRISE_MONTHLY_PRICE_ID,
        ('enterprise', 'yearly'): ENTERPRISE_YEARLY_PRICE_ID,
    }

    # ============================================================
    # SCHEDULER CONFIGURATION
    # ============================================================

    # Usage tracking scheduler
    USAGE_TRACKING_ENABLED: bool = os.getenv("USAGE_TRACKING_ENABLED", "true").lower() == "true"
    USAGE_TRACKING_HOUR: int = int(os.getenv("USAGE_TRACKING_HOUR", "2"))
    USAGE_TRACKING_MINUTE: int = int(os.getenv("USAGE_TRACKING_MINUTE", "0"))
    USAGE_TRACKING_TIMEZONE: str = os.getenv("USAGE_TRACKING_TIMEZONE", "UTC")

settings = TenantSettings()
