"""Two-dimension service catalog (STATE-WP-0062).

Every service is classified along two orthogonal dimensions:

  - hosting_type:     self_hosted (coulomb operates it) | cloud_hosted (consumed)
  - development_type: first_party (coulomb develops it) | third_party (external)

Common fields live in ``ServiceCatalog``; dimension-specific data composes via
1:1 extension tables (``service_id`` is both PK and FK), so a self-hosted
first-party service carries the self-hosted *and* first-party extensions without
needing a bespoke per-class shape.
"""
import uuid
from datetime import datetime

from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, func
from sqlalchemy.dialects.postgresql import JSON, UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from api.models.base import Base


class ServiceCatalog(Base):
    __tablename__ = "service_catalog"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    slug: Mapped[str] = mapped_column(String(100), nullable=False, unique=True, index=True)
    name: Mapped[str] = mapped_column(String(200), nullable=False)
    owner_or_provider: Mapped[str | None] = mapped_column(String(200), nullable=True)
    category: Mapped[str | None] = mapped_column(String(100), nullable=True)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    website_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    # status: active | deprecated
    status: Mapped[str] = mapped_column(String(20), nullable=False, server_default="active")
    # hosting_type: self_hosted | cloud_hosted
    hosting_type: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
    # development_type: first_party | third_party
    development_type: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
    # Service DoM Level (1=Operable, 2=Observable, 3=Mature); NULL = unassessed
    maturity_level: Mapped[int | None] = mapped_column(Integer, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    third_party: Mapped["ServiceThirdParty | None"] = relationship(
        back_populates="service", uselist=False, cascade="all, delete-orphan")
    first_party: Mapped["ServiceFirstParty | None"] = relationship(
        back_populates="service", uselist=False, cascade="all, delete-orphan")
    cloud: Mapped["ServiceCloud | None"] = relationship(
        back_populates="service", uselist=False, cascade="all, delete-orphan")
    self_hosted: Mapped["ServiceSelfHosted | None"] = relationship(
        back_populates="service", uselist=False, cascade="all, delete-orphan")


class ServiceThirdParty(Base):
    """Extension for development_type = third_party (coulomb is not dev-responsible)."""
    __tablename__ = "service_third_party"

    service_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True)
    # pricing_model: free | paid | freemium | usage_based | unknown
    pricing_model: Mapped[str] = mapped_column(String(20), nullable=False, server_default="unknown")
    upstream_packages: Mapped[list | None] = mapped_column(JSON, nullable=True)
    upstream_contacts: Mapped[list | None] = mapped_column(JSON, nullable=True)
    source_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    support_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    license: Mapped[str | None] = mapped_column(String(100), nullable=True)

    service: Mapped["ServiceCatalog"] = relationship(back_populates="third_party")


class ServiceFirstParty(Base):
    """Extension for development_type = first_party (coulomb develops it)."""
    __tablename__ = "service_first_party"

    service_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True)
    repo_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("managed_repos.id", ondelete="SET NULL"), nullable=True, index=True)
    owning_domain: Mapped[str | None] = mapped_column(String(100), nullable=True)

    service: Mapped["ServiceCatalog"] = relationship(back_populates="first_party")


class ServiceCloud(Base):
    """Extension for hosting_type = cloud_hosted (data is processed off coulomb infra).

    Holds the data-processor concerns that were the heart of the old TPSC record;
    they apply whenever data leaves coulomb infra, independent of who built it.
    """
    __tablename__ = "service_cloud"

    service_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True)
    # gdpr_maturity (CNIL/IAPP CMMI-aligned):
    # unknown | non_compliant | initial | developing | defined | managed | certified
    gdpr_maturity: Mapped[str] = mapped_column(String(20), nullable=False, server_default="unknown", index=True)
    gdpr_notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    dpa_available: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="false")
    tos_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    privacy_policy_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    data_processing_regions: Mapped[list | None] = mapped_column(JSON, nullable=True)
    data_retention_notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    service: Mapped["ServiceCatalog"] = relationship(back_populates="cloud")


class ServiceSelfHosted(Base):
    """Extension for hosting_type = self_hosted (coulomb operates the service)."""
    __tablename__ = "service_self_hosted"

    service_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("service_catalog.id", ondelete="CASCADE"), primary_key=True)
    # three-helix instance / host the service runs on
    helix_instance: Mapped[str | None] = mapped_column(String(100), nullable=True)
    host_node: Mapped[str | None] = mapped_column(String(100), nullable=True)
    deployment_ref: Mapped[str | None] = mapped_column(Text, nullable=True)
    runbook_ref: Mapped[str | None] = mapped_column(Text, nullable=True)
    # upstream OSS project when the self-hosted service is third-party software
    upstream_oss_project: Mapped[str | None] = mapped_column(String(200), nullable=True)

    service: Mapped["ServiceCatalog"] = relationship(back_populates="self_hosted")
