"""Migration #1: raw records, the accounts skeleton, and three honesty columns.

Schema only (build plan §2): every table and column here either is nullable
or carries a server default, and no data moves. The one seed row (rian's
account) is `app.cli backfill accounts`, run after deploy.

* accounts: the FK target every later `*_by` column points at, so who-columns
  have an identity from day one (Decision 2). Email nullable: guests exist.
* raw_records: the parsed fragment each listing was derived from, per run,
  so any rule can be re-run over what was actually seen (Decision 6: first
  principle, everything re-doable). Confirmed 2026-09-04 that nothing was kept.
* price_observations.source_kind: collector | browser | human, so a hand-entered
  or browser-rendered price is never mistaken for a collected one.
* sources.identity_mode + permission_record: a source runs `browser_like` only
  when the record names who agreed, when and how (agents.md, 2026-09-04).
* locations.visible now defaults to False: a newly collected airport stays
  hidden until switched on deliberately (Decision 1).
* collection_runs.fx_source + fx_fetched_at and price_observations.fx_rate:
  the rate a USD figure was derived with, and whether it was live or the
  fallback table. Before this a fallback run was indistinguishable from a
  live one once the log rotated.

Revision ID: 0a1b2c3d4e5f
Revises: b0c1d2e3f4a5
Create Date: 2026-09-04
"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

revision = "0a1b2c3d4e5f"
down_revision = "b0c1d2e3f4a5"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "accounts",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("email", sa.String(320), nullable=True, unique=True),
        sa.Column("display_name", sa.String(80), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
        ),
    )
    op.create_table(
        "raw_records",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("listing_id", sa.Integer(), sa.ForeignKey("listings.id"), nullable=False),
        sa.Column("run_id", sa.Integer(), sa.ForeignKey("collection_runs.id"), nullable=True),
        sa.Column("payload", postgresql.JSONB(), nullable=False),
        sa.Column("parser_version", sa.String(40), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
        ),
    )
    op.create_index("ix_raw_records_listing_id", "raw_records", ["listing_id"])
    op.create_index("ix_raw_records_run_id", "raw_records", ["run_id"])
    op.add_column(
        "price_observations",
        sa.Column("source_kind", sa.String(16), nullable=False, server_default="collector"),
    )
    op.add_column(
        "sources",
        sa.Column("identity_mode", sa.String(16), nullable=False, server_default="declared"),
    )
    op.add_column("sources", sa.Column("permission_record", sa.Text(), nullable=True))
    op.alter_column("locations", "visible", server_default=sa.false())
    op.add_column("collection_runs", sa.Column("fx_source", sa.String(16), nullable=True))
    op.add_column(
        "collection_runs", sa.Column("fx_fetched_at", sa.DateTime(timezone=True), nullable=True)
    )
    op.add_column("price_observations", sa.Column("fx_rate", sa.Numeric(14, 6), nullable=True))


def downgrade() -> None:
    op.drop_column("price_observations", "fx_rate")
    op.drop_column("collection_runs", "fx_fetched_at")
    op.drop_column("collection_runs", "fx_source")
    op.alter_column("locations", "visible", server_default=sa.true())
    op.drop_column("sources", "permission_record")
    op.drop_column("sources", "identity_mode")
    op.drop_column("price_observations", "source_kind")
    op.drop_index("ix_raw_records_run_id", table_name="raw_records")
    op.drop_index("ix_raw_records_listing_id", table_name="raw_records")
    op.drop_table("raw_records")
    op.drop_table("accounts")
