"""
Cache configuration from environment variables
"""
import os
from dataclasses import dataclass
from typing import Optional


@dataclass
class CacheConfig:
    """Cache configuration loaded from environment variables"""

    # Core Settings
    enabled: bool
    cache_type: str  # 'hybrid', 'memory', 'sqlite', 'disabled'
    sqlite_path: str
    memory_max_size: int
    default_ttl: int

    # TTL per Cache Type (seconds)
    ttl_schema: int
    ttl_companies: int
    ttl_dashboard_summary: int
    ttl_dashboard_trends: int
    ttl_invoices: int
    ttl_invoices_summary: int
    ttl_treasury: int
    ttl_trial_balance: int
    ttl_calendar_periods: int

    # Maintenance
    cleanup_interval: int

    # Event-Based Invalidation
    auto_invalidate_enabled: bool
    check_interval: int

    # Performance Tracking
    track_performance: bool
    benchmark_on_startup: bool

    @classmethod
    def from_env(cls) -> 'CacheConfig':
        """Load configuration from environment variables"""
        return cls(
            # Core Settings
            enabled=os.getenv('CACHE_ENABLED', 'True').lower() == 'true',
            cache_type=os.getenv('CACHE_TYPE', 'hybrid'),
            sqlite_path=os.getenv('CACHE_SQLITE_PATH', './data/cache/roa2web_cache.db'),
            memory_max_size=int(os.getenv('CACHE_MEMORY_MAX_SIZE', '1000')),
            default_ttl=int(os.getenv('CACHE_DEFAULT_TTL', '900')),

            # TTL per Cache Type
            ttl_schema=int(os.getenv('CACHE_TTL_SCHEMA', '86400')),
            ttl_companies=int(os.getenv('CACHE_TTL_COMPANIES', '1800')),
            ttl_dashboard_summary=int(os.getenv('CACHE_TTL_DASHBOARD_SUMMARY', '1800')),
            ttl_dashboard_trends=int(os.getenv('CACHE_TTL_DASHBOARD_TRENDS', '1800')),
            ttl_invoices=int(os.getenv('CACHE_TTL_INVOICES', '600')),
            ttl_invoices_summary=int(os.getenv('CACHE_TTL_INVOICES_SUMMARY', '900')),
            ttl_treasury=int(os.getenv('CACHE_TTL_TREASURY', '600')),
            ttl_trial_balance=int(os.getenv('CACHE_TTL_TRIAL_BALANCE', '600')),
            ttl_calendar_periods=int(os.getenv('CACHE_TTL_CALENDAR_PERIODS', '3600')),

            # Maintenance
            cleanup_interval=int(os.getenv('CACHE_CLEANUP_INTERVAL', '3600')),

            # Event-Based Invalidation
            auto_invalidate_enabled=os.getenv('CACHE_AUTO_INVALIDATE', 'False').lower() == 'true',
            check_interval=int(os.getenv('CACHE_CHECK_INTERVAL', '300')),

            # Performance Tracking
            track_performance=os.getenv('CACHE_TRACK_PERFORMANCE', 'True').lower() == 'true',
            benchmark_on_startup=os.getenv('CACHE_BENCHMARK_ON_STARTUP', 'True').lower() == 'true',
        )

    def get_ttl_for_type(self, cache_type: str) -> int:
        """Get TTL for specific cache type"""
        ttl_map = {
            'schema': self.ttl_schema,
            'companies': self.ttl_companies,
            'dashboard_summary': self.ttl_dashboard_summary,
            'dashboard_trends': self.ttl_dashboard_trends,
            'invoices': self.ttl_invoices,
            'invoices_summary': self.ttl_invoices_summary,
            'treasury': self.ttl_treasury,
            'trial_balance': self.ttl_trial_balance,
            'calendar_periods': self.ttl_calendar_periods,
        }
        return ttl_map.get(cache_type, self.default_ttl)
