"""Initial schema — tiles (mirrored from satellite-provider) + onboard tables.

Per `_docs/02_document/data_model.md § 2`. The `tiles` schema mirrors
`satellite-provider`'s canonical columns + the additive onboard-only columns;
the additive-only invariant (Principle #5) is enforced at the migration-review
level.

Revision ID: 0001_initial
Revises:
Create Date: 2026-05-11
"""

from __future__ import annotations

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

revision: str = "0001_initial"
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # flights -----------------------------------------------------------------
    op.create_table(
        "flights",
        sa.Column("id", sa.dialects.postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("companion_id", sa.Text(), nullable=False),
        sa.Column(
            "started_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
        sa.Column("landed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("metadata", sa.dialects.postgresql.JSONB(), nullable=True),
    )

    # sector_classifications --------------------------------------------------
    op.create_table(
        "sector_classifications",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        sa.Column("sector_id", sa.Text(), nullable=False, unique=True),
        sa.Column("classification", sa.Text(), nullable=False),
        sa.Column("freshness_threshold_days", sa.Integer(), nullable=False),
    )

    # tiles (mirrored canonical columns + additive onboard-only) --------------
    op.create_table(
        "tiles",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        # Canonical columns (mirrored from satellite-provider).
        sa.Column("zoom_level", sa.Integer(), nullable=False),
        sa.Column("tile_x", sa.Integer(), nullable=False),
        sa.Column("tile_y", sa.Integer(), nullable=False),
        sa.Column("latitude", sa.Float(), nullable=False),
        sa.Column("longitude", sa.Float(), nullable=False),
        sa.Column("tile_size_meters", sa.Float(), nullable=False),
        sa.Column("tile_size_pixels", sa.Integer(), nullable=False),
        sa.Column("capture_timestamp", sa.DateTime(timezone=True), nullable=False),
        sa.Column("compression", sa.Text(), nullable=False, server_default=sa.text("'jpeg'")),
        sa.Column("crs", sa.Text(), nullable=False, server_default=sa.text("'EPSG:3857'")),
        sa.Column("source", sa.Text(), nullable=False),
        # Additive onboard-only columns.
        sa.Column(
            "flight_id",
            sa.dialects.postgresql.UUID(as_uuid=True),
            sa.ForeignKey("flights.id"),
            nullable=True,
        ),
        sa.Column("companion_id", sa.Text(), nullable=True),
        sa.Column("tile_quality_metadata", sa.dialects.postgresql.JSONB(), nullable=True),
        sa.Column("voting_status", sa.Text(), nullable=True),
        sa.Column("freshness_status", sa.Text(), nullable=False, server_default=sa.text("'fresh'")),
        sa.Column("signature", sa.LargeBinary(), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
        sa.CheckConstraint("zoom_level BETWEEN 10 AND 22", name="ck_tiles_zoom"),
        sa.CheckConstraint("tile_size_meters > 0", name="ck_tiles_meters"),
        sa.CheckConstraint("tile_size_pixels > 0", name="ck_tiles_pixels"),
        sa.CheckConstraint("source IN ('googlemaps','onboard_ingest')", name="ck_tiles_source"),
        sa.CheckConstraint(
            "voting_status IS NULL OR voting_status IN ('pending','trusted','rejected')",
            name="ck_tiles_voting_status",
        ),
        sa.CheckConstraint(
            "freshness_status IN ('fresh','stale_warn','stale_reject')",
            name="ck_tiles_freshness_status",
        ),
    )

    op.create_index("ix_tiles_zxy", "tiles", ["zoom_level", "tile_x", "tile_y"])
    op.create_index("ix_tiles_lat_lon", "tiles", ["latitude", "longitude"])
    op.create_index(
        "ix_tiles_voting_status_onboard",
        "tiles",
        ["voting_status"],
        postgresql_where=sa.text("source = 'onboard_ingest'"),
    )
    op.create_index("ix_tiles_flight_id", "tiles", ["flight_id"])
    op.create_index("ix_tiles_created_at", "tiles", ["created_at"])

    # manifests ---------------------------------------------------------------
    op.create_table(
        "manifests",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        sa.Column("manifest_id", sa.Text(), nullable=False, unique=True),
        sa.Column(
            "flight_id",
            sa.dialects.postgresql.UUID(as_uuid=True),
            sa.ForeignKey("flights.id"),
            nullable=False,
        ),
        sa.Column("content_hash", sa.Text(), nullable=False),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
        sa.Column("payload", sa.dialects.postgresql.JSONB(), nullable=False),
    )

    # engine_cache_entries ----------------------------------------------------
    op.create_table(
        "engine_cache_entries",
        sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
        sa.Column("engine_path", sa.Text(), nullable=False),
        sa.Column("sm_arch", sa.Text(), nullable=False),
        sa.Column("jetpack_version", sa.Text(), nullable=False),
        sa.Column("tensorrt_version", sa.Text(), nullable=False),
        sa.Column("precision", sa.Text(), nullable=False),
        sa.Column("content_hash", sa.Text(), nullable=False, unique=True),
        sa.Column("int8_calibration_path", sa.Text(), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
    )


def downgrade() -> None:
    op.drop_table("engine_cache_entries")
    op.drop_table("manifests")
    op.drop_index("ix_tiles_created_at", table_name="tiles")
    op.drop_index("ix_tiles_flight_id", table_name="tiles")
    op.drop_index("ix_tiles_voting_status_onboard", table_name="tiles")
    op.drop_index("ix_tiles_lat_lon", table_name="tiles")
    op.drop_index("ix_tiles_zxy", table_name="tiles")
    op.drop_table("tiles")
    op.drop_table("sector_classifications")
    op.drop_table("flights")
