"""Public memorial page fields (INDL-46 Public Memorial Page)

Adds:
- records: birthplace, city_of_residence, occupation  (quick-facts bar, AC-10)
- memorials: candle_count, biography_ai_generated, service_date, service_location
  (candle button + AI attribution note + quick-facts "Service", AC-05/07/09/12/10)

The life-timeline (AC-22/23) reuses the existing ``memorial_timeline_events``
table (event_date/title/description/sort_order) — no new table is required, so
the PRD's proposed ``memorial_timeline_entries`` table is intentionally omitted.

NOTE (2026-07-03, INDL-52): this file originally claimed revision id "0057"
with down_revision "0056", independently of 0057_cms_pages_add_content.py
which also claimed "0057" off "0056" — both were merged into `develop`
without noticing the collision, and only the cms_pages branch was ever
actually applied to existing databases (confirmed via `alembic current` +
inspecting information_schema on the dev DB before this fix). Renumbered to
"0058a", chained after 0058 (account_status_canceled_spelling), following
this repo's existing convention for resolving revision-id collisions
(see 0004a, 0007b, 0038a). No DDL below was changed.
"""
from alembic import op
import sqlalchemy as sa

revision = "0058a"
down_revision = "0058"
branch_labels = None
depends_on = None


def upgrade() -> None:
    conn = op.get_bind()
    # records — quick-facts bar
    conn.execute(sa.text(
        "ALTER TABLE records ADD COLUMN IF NOT EXISTS birthplace VARCHAR(255) NULL"
    ))
    conn.execute(sa.text(
        "ALTER TABLE records ADD COLUMN IF NOT EXISTS city_of_residence VARCHAR(255) NULL"
    ))
    conn.execute(sa.text(
        "ALTER TABLE records ADD COLUMN IF NOT EXISTS occupation VARCHAR(255) NULL"
    ))
    # memorials — candle count, AI-attribution flag, service details
    conn.execute(sa.text(
        "ALTER TABLE memorials ADD COLUMN IF NOT EXISTS candle_count INTEGER NOT NULL DEFAULT 0"
    ))
    conn.execute(sa.text(
        "ALTER TABLE memorials ADD COLUMN IF NOT EXISTS biography_ai_generated BOOLEAN NOT NULL DEFAULT FALSE"
    ))
    conn.execute(sa.text(
        "ALTER TABLE memorials ADD COLUMN IF NOT EXISTS service_date DATE NULL"
    ))
    conn.execute(sa.text(
        "ALTER TABLE memorials ADD COLUMN IF NOT EXISTS service_location VARCHAR(255) NULL"
    ))


def downgrade() -> None:
    conn = op.get_bind()
    conn.execute(sa.text("ALTER TABLE memorials DROP COLUMN IF EXISTS service_location"))
    conn.execute(sa.text("ALTER TABLE memorials DROP COLUMN IF EXISTS service_date"))
    conn.execute(sa.text("ALTER TABLE memorials DROP COLUMN IF EXISTS biography_ai_generated"))
    conn.execute(sa.text("ALTER TABLE memorials DROP COLUMN IF EXISTS candle_count"))
    conn.execute(sa.text("ALTER TABLE records DROP COLUMN IF EXISTS occupation"))
    conn.execute(sa.text("ALTER TABLE records DROP COLUMN IF EXISTS city_of_residence"))
    conn.execute(sa.text("ALTER TABLE records DROP COLUMN IF EXISTS birthplace"))
