#!/usr/bin/env python3
"""Idempotent seed for service_registry only (commercial SoT is Commercial Runtime)."""
from __future__ import annotations

import asyncio

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine

from app.core.config import settings
from app.models.enums import ServiceStatus
from app.models.registry import ServiceRegistry

PLATFORM_SERVICES: list[dict[str, str | None]] = [
    {
        "service_key": "accounting",
        "name": "حسابداری آنلاین",
        "base_url": "http://accounting-service:8002",
        "health_check_url": "http://accounting-service:8002/health",
    },
    {
        "service_key": "healthcare",
        "name": "تربت هلث",
        "base_url": "http://healthcare-service:8010",
        "health_check_url": "http://healthcare-service:8010/health",
    },
    {
        "service_key": "beauty",
        "name": "تربت بیوتی",
        "base_url": "http://beauty-business-service:8011",
        "health_check_url": "http://beauty-business-service:8011/health",
    },
    {
        "service_key": "crm",
        "name": "سیستم CRM",
        "base_url": "http://crm-service:8003",
        "health_check_url": "http://crm-service:8003/health",
    },
    {
        "service_key": "loyalty",
        "name": "تربت وفاداری",
        "base_url": "http://loyalty-service:8004",
        "health_check_url": "http://loyalty-service:8004/health",
    },
    {
        "service_key": "communication",
        "name": "ارتباطات و پیامک",
        "base_url": "http://communication-service:8005",
        "health_check_url": "http://communication-service:8005/health",
    },
    {
        "service_key": "sports_center",
        "name": "مرکز ورزشی",
        "base_url": "http://sports-center-service:8006",
        "health_check_url": "http://sports-center-service:8006/health",
    },
    {
        "service_key": "delivery",
        "name": "تربت درایور",
        "base_url": "http://delivery-service:8007",
        "health_check_url": "http://delivery-service:8007/health",
    },
    {
        "service_key": "experience",
        "name": "پلتفرم تجربه",
        "base_url": "http://experience-service:8008",
        "health_check_url": "http://experience-service:8008/health",
    },
    {
        "service_key": "hospitality",
        "name": "تربت فود",
        "base_url": "http://hospitality-service:8009",
        "health_check_url": "http://hospitality-service:8009/health",
    },
    {
        "service_key": "identity",
        "name": "هویت و دسترسی",
        "base_url": "http://identity-access-service:8001",
        "health_check_url": "http://identity-access-service:8001/health",
    },
]


async def seed(session: AsyncSession) -> tuple[int, int]:
    services_added = 0

    for row in PLATFORM_SERVICES:
        existing = await session.scalar(
            select(ServiceRegistry).where(ServiceRegistry.service_key == row["service_key"])
        )
        if existing is None:
            session.add(
                ServiceRegistry(
                    service_key=str(row["service_key"]),
                    name=str(row["name"]),
                    base_url=row["base_url"],
                    health_check_url=row["health_check_url"],
                    status=ServiceStatus.ACTIVE,
                )
            )
            services_added += 1

    await session.commit()
    return services_added, 0


async def main() -> None:
    engine = create_async_engine(settings.database_url)
    Session = async_sessionmaker(engine, expire_on_commit=False)
    async with Session() as session:
        services_added, _ = await seed(session)
        print(f"seed_platform_catalog: services_added={services_added}")
    await engine.dispose()


if __name__ == "__main__":
    asyncio.run(main())
