from datetime import datetime

from sqlalchemy import String, Integer, Float, Boolean, DateTime, Text, JSON
from sqlalchemy.orm import Mapped, mapped_column

from backend.database.connection import Base


class AppSettings(Base):
    """Platform configuration stored in database."""
    __tablename__ = "app_settings"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    key: Mapped[str] = mapped_column(String(100), unique=True, index=True)
    value: Mapped[str] = mapped_column(Text, default="")
    category: Mapped[str] = mapped_column(String(50), default="general", index=True)
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
    )


# Default settings with categories
DEFAULT_SETTINGS = {
    # ── General ──
    "app_name": {"value": "Viral Social Pro", "category": "general"},
    "app_language": {"value": "pt-BR", "category": "general"},
    "timezone": {"value": "America/Sao_Paulo", "category": "general"},
    "default_region": {"value": "BR", "category": "general"},
    "dark_mode": {"value": "true", "category": "general"},
    "brand_color": {"value": "#7c3aed", "category": "general"},
    "items_per_page": {"value": "24", "category": "general"},

    # ── API Keys ──
    "scrapecreators_api_key": {"value": "", "category": "api_keys"},
    "youtube_api_key": {"value": "", "category": "api_keys"},
    "apify_api_token": {"value": "", "category": "api_keys"},
    "sociavault_api_key": {"value": "", "category": "api_keys"},
    "echotik_username": {"value": "260310961666490322", "category": "api_keys"},
    "echotik_api_key": {"value": "c87ca52456c2474a9077be12bf8c66ff", "category": "api_keys"},

    # ── Discovery ──
    "discovery_auto_enabled": {"value": "false", "category": "discovery"},
    "discovery_interval_hours": {"value": "6", "category": "discovery"},
    "discovery_max_per_platform": {"value": "25", "category": "discovery"},
    "discovery_platforms": {"value": "tiktok,instagram,youtube", "category": "discovery"},
    "discovery_min_views": {"value": "10000", "category": "discovery"},
    "discovery_min_likes": {"value": "500", "category": "discovery"},
    "discovery_keywords": {"value": "viral,trending,moda feminina,skincare,gadgets", "category": "discovery"},

    # ── Viral Score ──
    "viral_score_weight_views": {"value": "0.30", "category": "viral_score"},
    "viral_score_weight_likes": {"value": "0.25", "category": "viral_score"},
    "viral_score_weight_comments": {"value": "0.20", "category": "viral_score"},
    "viral_score_weight_shares": {"value": "0.15", "category": "viral_score"},
    "viral_score_weight_engagement": {"value": "0.10", "category": "viral_score"},
    "viral_score_min_display": {"value": "30", "category": "viral_score"},
    "viral_score_threshold_hot": {"value": "70", "category": "viral_score"},
    "viral_score_threshold_fire": {"value": "85", "category": "viral_score"},

    # ── TikTok Shop ──
    "shop_auto_monitor": {"value": "false", "category": "shop"},
    "shop_snapshot_interval_hours": {"value": "6", "category": "shop"},
    "shop_min_sold_count": {"value": "100", "category": "shop"},
    "shop_categories_br": {"value": "moda,beleza,casa,eletronicos,esportes", "category": "shop"},
    "shop_auto_detect_enabled": {"value": "true", "category": "shop"},
    "shop_auto_detect_interval_hours": {"value": "6", "category": "shop"},
    "shop_max_products_per_cycle": {"value": "3", "category": "shop"},
    "shop_cross_platform_keywords": {"value": "moda feminina,look barato,skincare barato,maquiagem viral,organizador casa,gadgets cozinha,gadgets baratos,fone bluetooth,legging fitness,tenis barato", "category": "shop"},

    # ── Notifications / Alerts ──
    "alerts_enabled": {"value": "true", "category": "alerts"},
    "alert_viral_threshold": {"value": "80", "category": "alerts"},
    "alert_views_spike": {"value": "100000", "category": "alerts"},
    "alert_new_trending": {"value": "true", "category": "alerts"},
    "alert_product_sales_spike": {"value": "50", "category": "alerts"},
    "alert_sound_enabled": {"value": "false", "category": "alerts"},

    # ── Content Calendar ──
    "calendar_platforms": {"value": "tiktok,instagram,youtube", "category": "calendar"},
    "calendar_posts_per_day": {"value": "3", "category": "calendar"},
    "calendar_best_time_analysis_days": {"value": "30", "category": "calendar"},

    # ── Downloads ──
    "download_path": {"value": "/home/glauko/viral-social/downloads", "category": "downloads"},
    "download_max_concurrent": {"value": "3", "category": "downloads"},
    "download_auto_thumbnail": {"value": "true", "category": "downloads"},
    "download_watermark_remove": {"value": "true", "category": "downloads"},
    "download_max_resolution": {"value": "1080", "category": "downloads"},

    # ── Backup ──
    "backup_auto_enabled": {"value": "true", "category": "backup"},
    "backup_interval_hours": {"value": "24", "category": "backup"},
    "backup_keep_days": {"value": "30", "category": "backup"},
    "backup_push_github": {"value": "true", "category": "backup"},

    # ── Feed ──
    "feed_auto_refresh": {"value": "true", "category": "feed"},
    "feed_refresh_interval_seconds": {"value": "300", "category": "feed"},
    "feed_default_sort": {"value": "viral_score", "category": "feed"},
    "feed_show_platform_badges": {"value": "true", "category": "feed"},
    "feed_card_style": {"value": "compact", "category": "feed"},
    "feed_show_engagement_rate": {"value": "true", "category": "feed"},

    # ── Creators ──
    "creators_auto_track": {"value": "false", "category": "creators"},
    "creators_min_followers": {"value": "1000", "category": "creators"},
    "creators_growth_alert_percent": {"value": "10", "category": "creators"},

    # -- Viral Scanner --
    "scanner_enabled": {"value": "true", "category": "scanner"},
    "scanner_platforms": {"value": "tiktok,youtube", "category": "scanner"},
    "scanner_interval_minutes": {"value": "15", "category": "scanner"},
    "scanner_keywords": {"value": "ai house build timelapse,ai construction luxury mansion,ai generated house building,sora house construction,ai mansion timelapse,ai dream house build,ai architecture timelapse,ai luxury home construction,ai building house from scratch,runway ai house,ai casa construção,casa ia timelapse,construção ia mansão,ai house transformation,ai modern house build,ai villa construction timelapse,ai home design timelapse,ai building process satisfying,ai generated construction,house built by ai timelapse,ai mega mansion,ai futuristic house build,ai impossible house construction,ai render house timelapse,ai house shorts viral", "category": "scanner"},
    "scanner_max_keywords_per_cycle": {"value": "10", "category": "scanner"},
    "scanner_results_per_keyword": {"value": "30", "category": "scanner"},
    "scanner_min_views_to_save": {"value": "50000", "category": "scanner"},
    "scanner_min_viral_score": {"value": "0", "category": "scanner"},
    "scanner_only_with_product": {"value": "false", "category": "scanner"},
    "scanner_daily_credit_limit": {"value": "200", "category": "scanner"},
    "scanner_api_delay_seconds": {"value": "1", "category": "scanner"},
    "scanner_creative_center_enabled": {"value": "true", "category": "scanner"},
    "scanner_creative_center_interval_minutes": {"value": "30", "category": "scanner"},
    "scanner_auto_detect_products": {"value": "true", "category": "scanner"},
    "scanner_dedup_enabled": {"value": "true", "category": "scanner"},
    "scanner_alert_mega_viral": {"value": "true", "category": "scanner"},

    # ── Trends Scanner ──
    "trends_scanner_enabled": {"value": "true", "category": "trends"},
    "trends_scanner_interval_minutes": {"value": "15", "category": "trends"},
    "trends_daily_credit_limit": {"value": "200", "category": "trends"},
    "trends_min_views_to_save": {"value": "10000", "category": "trends"},
    "trends_region": {"value": "BR", "category": "trends"},
    "trends_max_results": {"value": "30", "category": "trends"},
    "trends_scan_tiktok": {"value": "true", "category": "trends"},
    "trends_scan_youtube": {"value": "true", "category": "trends"},
    "trends_api_delay_seconds": {"value": "2", "category": "trends"},

    # ── Advanced ──
    "api_rate_limit_per_minute": {"value": "60", "category": "advanced"},
    "cache_ttl_seconds": {"value": "300", "category": "advanced"},
    "log_level": {"value": "INFO", "category": "advanced"},
    "debug_mode": {"value": "false", "category": "advanced"},
    "data_retention_days": {"value": "90", "category": "advanced"},
}
