"""Doris migration: create web_parsed_pages warehouse table.

Stores parsed web page content for analytics — identical columns to
the PostgreSQL table for full parity.

Uses UNIQUE KEY on ``url`` so re-parsing the same URL upserts in-place.
Distributed by ``url`` with 16 buckets — distribution column must be
part of the UNIQUE KEY in Doris.
"""

from __future__ import annotations

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

VERSION = "0008"


def get_doris_migrations() -> list[Migration]:
    """Return ordered Doris migrations."""
    return [
        Migration(
            version="parsed_page_doris_0001",
            description="Create parsed_pages warehouse table for web-parser analytics",
            dialect="doris",
            up=(
                MigrationBuilder("web_parsed_pages", dialect="doris")
                # url must come first — it's the UNIQUE KEY column (VARCHAR, not TEXT)
                .varchar("url", 2048, nullable=False)
                .uuid("id", 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)
                # -- Metadata --
                .varchar("parsed_at", 64, nullable=True)
                .jsonb("diagnostics", nullable=True)
                # -- Timestamps --
                .varchar("created_at", 64, nullable=True)
                .varchar("updated_at", 64, nullable=True)
                # -- Keys & distribution --
                .doris_unique_key(["url"])
                .doris_distributed_by("url", buckets=16)
                .create_table()
            ),
            down=MigrationBuilder("web_parsed_pages", dialect="doris").drop_table(),
        ),
    ]
