"""Doris migration: create web_media warehouse table.

Stores web media download metadata for analytics — columns mirror the
PostgreSQL table for full parity.

Uses DUPLICATE KEY on ``id`` so every download creates a new row.
Distributed by ``source_url`` with 16 buckets.
"""

from __future__ import annotations

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

VERSION = "0010"


def get_doris_migrations() -> list[Migration]:
    """Return ordered Doris migrations."""
    return [
        Migration(
            version="web_media_doris_0001",
            description="Create web_media warehouse table for web media analytics",
            dialect="doris",
            up=(
                MigrationBuilder("web_media", dialect="doris")
                .varchar("id", 64, nullable=False)
                .text("source_url", nullable=False)
                .text("parent_url", nullable=False)
                .varchar("seed_id", 64, nullable=False)
                # -- Classification --
                .text("media_type", nullable=False)
                # -- Storage --
                .text("storage_bucket", nullable=False)
                .text("storage_key", nullable=False)
                # -- Content metadata --
                .text("content_hash", nullable=False)
                .text("content_type", nullable=True)
                .bigint("content_length", nullable=False)
                .text("original_filename", nullable=True)
                # -- Extra context --
                .jsonb("metadata", nullable=True)
                # -- Timestamps --
                .varchar("created_at", 64, nullable=True)
                .varchar("updated_at", 64, nullable=True)
                # -- Keys & distribution --
                .doris_duplicate_key(["id"])
                .doris_distributed_by("source_url", buckets=16)
                .create_table()
            ),
            down=MigrationBuilder("web_media", dialect="doris").drop_table(),
        ),
    ]
