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 TPSCCatalog(Base):
    __tablename__ = "tpsc_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)
    provider: Mapped[str | None] = mapped_column(String(200), nullable=True)
    category: Mapped[str | None] = mapped_column(String(100), nullable=True)
    website_url: Mapped[str | None] = mapped_column(Text, nullable=True)
    # Pricing: free | paid | freemium | usage_based | unknown
    pricing_model: Mapped[str] = mapped_column(String(20), nullable=False, server_default="unknown")
    # 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)
    # status: active | deprecated
    status: Mapped[str] = mapped_column(String(20), nullable=False, server_default="active")
    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())

    entries: Mapped[list["TPSCEntry"]] = relationship("TPSCEntry", back_populates="catalog_entry")


class TPSCSnapshot(Base):
    __tablename__ = "tpsc_snapshots"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    repo_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("managed_repos.id", ondelete="SET NULL"), nullable=True, index=True)
    snapshot_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    source_file: Mapped[str | None] = mapped_column(String(200), nullable=True)
    entry_count: Mapped[int] = mapped_column(Integer, nullable=False, server_default="0")

    entries: Mapped[list["TPSCEntry"]] = relationship("TPSCEntry", back_populates="snapshot", cascade="all, delete-orphan")


class TPSCEntry(Base):
    __tablename__ = "tpsc_entries"

    id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    snapshot_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("tpsc_snapshots.id", ondelete="CASCADE"), nullable=False, index=True)
    catalog_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("tpsc_catalog.id", ondelete="SET NULL"), nullable=True)
    service_slug: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
    purpose: Mapped[str | None] = mapped_column(Text, nullable=True)
    # auth_type: api_key | oauth | cli | none | unknown
    auth_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
    endpoint_override: Mapped[str | None] = mapped_column(Text, nullable=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    snapshot: Mapped["TPSCSnapshot"] = relationship("TPSCSnapshot", back_populates="entries")
    catalog_entry: Mapped["TPSCCatalog | None"] = relationship("TPSCCatalog", back_populates="entries")
