"""平台预设种子数据

首次部署或调用 POST /api/providers/seed 时初始化常见平台。
每个平台有唯一 slug，供未来按 sdk_type 对接自动同步扩展。
"""

from sqlmodel import Session, select

from app.models.provider import Provider

PRESET_PROVIDERS = [
    # ---- VPS / 云服务商 ----
    # 综合平台：services 列出可提供的服务，卡片上会显示多标签
    {"slug": "aliyun", "name": "阿里云", "name_en": "Aliyun", "category": "vps",
     "services": "vps,domain,ssl_cert,cdn,dns",
     "website": "https://aliyun.com", "console_url": "https://ecs.console.aliyun.com", "sdk_type": "aliyun-sdk"},
    {"slug": "tencent", "name": "腾讯云", "name_en": "Tencent Cloud", "category": "vps",
     "services": "vps,domain,ssl_cert,cdn,dns",
     "website": "https://cloud.tencent.com", "console_url": "https://console.cloud.tencent.com/cvm", "sdk_type": "tencent-sdk"},
    {"slug": "aliyun-intl", "name": "阿里国际", "name_en": "Alibaba Cloud", "category": "vps",
     "services": "vps,domain,ssl_cert,cdn,dns",
     "website": "https://alibabacloud.com", "console_url": "https://ecs.console.alibabacloud.com", "sdk_type": "alibabacloud-sdk"},
    {"slug": "tencent-intl", "name": "腾讯国际", "name_en": "Tencent Cloud Intl", "category": "vps",
     "services": "vps,domain,ssl_cert,cdn,dns",
     "website": "https://intl.cloud.tencent.com", "console_url": "https://console.intl.cloud.tencent.com", "sdk_type": "tencent-intl-sdk"},
    {"slug": "vultr", "name": "Vultr", "name_en": "Vultr", "category": "vps",
     "services": "vps",
     "website": "https://vultr.com", "console_url": "https://my.vultr.com", "sdk_type": "vultr-api"},
    {"slug": "digitalocean", "name": "DigitalOcean", "name_en": "DigitalOcean", "category": "vps",
     "services": "vps,domain",
     "website": "https://digitalocean.com", "console_url": "https://cloud.digitalocean.com", "sdk_type": "do-api"},
    {"slug": "linode", "name": "Linode", "name_en": "Akamai Linode", "category": "vps",
     "services": "vps",
     "website": "https://linode.com", "console_url": "https://cloud.linode.com", "sdk_type": "linode-api"},
    {"slug": "cloudcone", "name": "CloudCone", "name_en": "CloudCone", "category": "vps",
     "services": "vps",
     "website": "https://cloudcone.com", "console_url": "https://app.cloudcone.com", "sdk_type": "cloudcone-api"},
    {"slug": "zeabur", "name": "Zeabur", "name_en": "Zeabur", "category": "vps",
     "services": "vps,domain",
     "website": "https://zeabur.com", "console_url": "https://dash.zeabur.com", "sdk_type": "zeabur-api"},
    # ---- 域名注册商 ----
    {"slug": "namesilo", "name": "Namesilo", "name_en": "Namesilo", "category": "domain",
     "services": "domain,dns",
     "website": "https://namesilo.com", "console_url": "https://www.namesilo.com/account_domains.php", "sdk_type": "namesilo-api"},
    {"slug": "xinwang", "name": "新网", "name_en": "Xinnet", "category": "domain",
     "services": "domain,dns",
     "website": "https://xinnet.com", "console_url": "https://www.xinnet.com", "sdk_type": None},
    {"slug": "dynadot", "name": "Dynadot", "name_en": "Dynadot", "category": "domain",
     "services": "domain,dns",
     "website": "https://dynadot.com", "console_url": "https://www.dynadot.com/account/domains", "sdk_type": "dynadot-api"},
    # ---- AI 服务商 ----
    {"slug": "openai", "name": "OpenAI", "name_en": "OpenAI", "category": "ai_agent",
     "services": "ai_agent",
     "website": "https://openai.com", "console_url": "https://platform.openai.com", "sdk_type": "openai-api"},
    {"slug": "minimax", "name": "Minimax", "name_en": "Minimax", "category": "ai_agent",
     "services": "ai_agent",
     "website": "https://minimax.io", "console_url": "https://platform.minimaxi.com", "sdk_type": "minimax-api"},
    {"slug": "kimi", "name": "Kimi", "name_en": "Moonshot", "category": "ai_agent",
     "services": "ai_agent",
     "website": "https://moonshot.cn", "console_url": "https://platform.moonshot.cn", "sdk_type": "moonshot-api"},
    {"slug": "agnes", "name": "Agnes", "name_en": "Agnes", "category": "ai_agent",
     "services": "ai_agent",
     "website": None, "console_url": None, "sdk_type": None},
    {"slug": "deepseek", "name": "DeepSeek", "name_en": "DeepSeek", "category": "ai_agent",
     "services": "ai_agent",
     "website": "https://deepseek.com", "console_url": "https://platform.deepseek.com", "sdk_type": "deepseek-api"},
    # ---- Cloudflare ----
    {"slug": "cloudflare", "name": "Cloudflare", "name_en": "Cloudflare", "category": "cloudflare",
     "services": "cloudflare,dns,ssl_cert,cdn",
     "website": "https://cloudflare.com", "console_url": "https://dash.cloudflare.com", "sdk_type": "cloudflare-api"},
]


def seed_providers(session: Session) -> int:
    """初始化预设平台（已存在的 slug 跳过），返回新增数量

    单次查询获取全部已存在 slug，避免逐条 SELECT 的 N 次往返。
    """
    existing_slugs = set(session.exec(select(Provider.slug)).all())
    added = 0
    for data in PRESET_PROVIDERS:
        if data["slug"] not in existing_slugs:
            session.add(Provider(**data))
            added += 1
    if added:
        session.commit()
    return added
