"""Doris migration: recreate web_parsed_pages for append-only versioning.

Changes from ``UNIQUE KEY(url)`` to ``DUPLICATE KEY(id)`` so every
parse creates a new row — matching the ``web_html_raw`` append-only
versioning pattern.  Adds a ``version`` column for monotonically
increasing content version tracking per URL.

The old table is dropped and recreated because:
- Doris does not support altering the key model on an existing table.
- Existing data was already corrupted by the failed upsert pattern
  (UNIQUE KEY(url) → UPDATE → "Only value columns of unique table
  could be updated" errors).  Raw HTML is preserved in ``web_html_raw``
  and can be re-parsed.

Revision ID: 0017
"""
from __future__ import annotations

from repo_core.migrations.builder import MigrationBuilder, RawSqlOp
from repo_core.migrations.runner import Migration

VERSION = "0017"


def get_doris_migrations() -> list[Migration]:
    """Return ordered Doris migrations."""
    return [
        Migration(
            version="parsed_page_versioning_doris_0001",
            description=(
                "Recreate web_parsed_pages with DUPLICATE KEY on id "
                "(append-only) and add version column"
            ),
            dialect="doris",
            up=RawSqlOp([
                # Step 1: Drop the broken table (UNIQUE KEY on url)
                "DROP TABLE IF EXISTS `web_parsed_pages`",

                # Step 2: Create new table — DUPLICATE KEY on id, append-only
                (
                    MigrationBuilder("web_parsed_pages", dialect="doris")
                    # url is no longer the key — just a regular VARCHAR column
                    .uuid("id", nullable=False)
                    .varchar("url", 2048, nullable=False)
                    .varchar("seed_id", 64, nullable=False)
                    # -- Core parsed fields --
                    .text("title", nullable=True)
                    .text("description", nullable=True)
                    .text("author", nullable=True)
                    .text("published_time", nullable=True)
                    .text("main_image", nullable=True)
                    # -- Content --
                    .text("content", nullable=True)
                    .text("content_html", nullable=True)
                    # -- Classification --
                    .jsonb("keywords", nullable=True)
                    .jsonb("tags", nullable=True)
                    .text("category", nullable=True)
                    # -- Structured data --
                    .jsonb("images", nullable=True)
                    .jsonb("tables", nullable=True)
                    .jsonb("links", nullable=True)
                    .jsonb("schema_data", nullable=True)
                    .jsonb("statistics", nullable=True)
                    # -- Custom / extension data --
                    .jsonb("custom_content", nullable=True)
                    # -- Metadata --
                    .varchar("parsed_at", 64, nullable=True)
                    .jsonb("diagnostics", nullable=True)
                    # -- Versioning (new) --
                    .integer("version", nullable=True, default=1)
                    # -- Timestamps --
                    .varchar("created_at", 64, nullable=True)
                    .varchar("updated_at", 64, nullable=True)
                    # -- Keys & distribution --
                    .doris_duplicate_key(["id"])
                    .doris_distributed_by("id", buckets=16)
                    .create_table()
                    .sql()[0]
                ),
            ]),
            down=RawSqlOp([
                # Rollback: revert to old UNIQUE KEY(url) schema (lossy — version info dropped)
                "DROP TABLE IF EXISTS `web_parsed_pages`",
                (
                    MigrationBuilder("web_parsed_pages", dialect="doris")
                    .uuid("id", nullable=False)
                    .varchar("url", 2048, nullable=False)
                    .varchar("seed_id", 64, nullable=False)
                    .text("title", nullable=True)
                    .text("description", nullable=True)
                    .text("author", nullable=True)
                    .text("published_time", nullable=True)
                    .text("main_image", nullable=True)
                    .text("content", nullable=True)
                    .text("content_html", nullable=True)
                    .jsonb("keywords", nullable=True)
                    .jsonb("tags", nullable=True)
                    .text("category", nullable=True)
                    .jsonb("images", nullable=True)
                    .jsonb("tables", nullable=True)
                    .jsonb("links", nullable=True)
                    .jsonb("schema_data", nullable=True)
                    .jsonb("statistics", nullable=True)
                    .jsonb("custom_content", nullable=True)
                    .varchar("parsed_at", 64, nullable=True)
                    .jsonb("diagnostics", nullable=True)
                    .varchar("created_at", 64, nullable=True)
                    .varchar("updated_at", 64, nullable=True)
                    .doris_unique_key(["url"])
                    .doris_distributed_by("url", buckets=16)
                    .create_table()
                    .sql()[0]
                ),
            ]),
        ),
    ]
