"""drop chat / journal / push / curator / weather tables

Revision ID: 0053
Revises: 0052
Create Date: 2026-05-27

Phase 9 of the MCP-first pivot. The Python models for these tables were
deleted in Phase 8 (commit 91bafb6); this migration drops the orphan
SQL tables and cleans up dead per-user setting rows.

Hard-cutover: existing rows in these tables are discarded. The
accompanying spec at docs/superpowers/specs/2026-05-26-mcp-first-pivot-design.md
explicitly accepted this loss.
"""
from alembic import op


revision = "0053"
down_revision = "0052"
branch_labels = None
depends_on = None


# Order: junction tables first so the parent tables don't trip FK drops
# even with CASCADE — keeps the migration readable.
_DEAD_TABLES = [
    # Moments + junction tables (journal entity links)
    "moment_notes",
    "moment_tasks",
    "moment_places",
    "moment_people",
    "moment_embeddings",
    "moments",
    # Chat / generation telemetry
    "generation_tool_log",
    "messages",
    "conversations",
    # Curator approval queue
    "pending_curator_actions",
    # Push notifications
    "push_subscriptions",
    # Weather cache (journal-prep background fetcher)
    "weather_cache",
    # Legacy RSS-item embeddings (pre-MCP pivot, briefly experimented with)
    "rss_item_embeddings",
]


# Specific setting keys to drop. Anything matching the LIKE patterns in
# upgrade() is also nuked; the explicit list catches one-off keys that
# don't fit the namespaced patterns.
_DEAD_SETTING_KEYS = [
    "default_model",
    "background_model",
    "assistant_name",
    "auto_consolidate_tasks",
    "chat_retention_days",
    "think_enabled",
    "rag_default_scope",
]


def upgrade() -> None:
    for table in _DEAD_TABLES:
        op.execute(f"DROP TABLE IF EXISTS {table} CASCADE")

    keys_csv = ",".join(f"'{k}'" for k in _DEAD_SETTING_KEYS)
    op.execute(
        "DELETE FROM settings WHERE "
        "key LIKE 'voice_%' OR "
        "key LIKE 'journal_%' OR "
        "key LIKE 'briefing_%' OR "
        "key LIKE 'curator_%' OR "
        f"key IN ({keys_csv})"
    )


def downgrade() -> None:
    raise NotImplementedError(
        "No downgrade — hard cutover per the MCP-first pivot spec. "
        "Rolling back is not supported at the database layer."
    )
