"""Doris warehouse migration: create telegram_media_files table.

Warehouse table for analytics queries on media download statistics:
bytes per channel, media type distribution, download velocity over time.
"""

from __future__ import annotations

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

VERSION = "0003"


def get_doris_migrations() -> list[Migration]:
    """Return the CREATE TABLE migration for telegram_media_files warehouse."""
    return [
        Migration(
            version="tg_media_file_doris_0001",
            description="Create telegram_media_files warehouse table for media analytics",
            dialect="doris",
            up=(
                MigrationBuilder("telegram_media_files", dialect="doris")
                # -- Composite logical FK to telegram_messages -----------
                .bigint("peer_id", nullable=False)
                .bigint("message_id", nullable=False)
                # -- S3 storage references -------------------------------
                .text("storage_key", nullable=False)
                .text("bucket", nullable=False)
                # -- File metadata ---------------------------------------
                .bigint("file_size", nullable=False)
                .varchar("mime_type", 100, nullable=False)
                .varchar("media_type", 20, nullable=False)
                # -- Optional dimensions / duration ----------------------
                .integer("width", nullable=True)
                .integer("height", nullable=True)
                .float_("duration_seconds", nullable=True)
                # -- Timestamps ------------------------------------------
                .timestamp("downloaded_at", nullable=False)
                .timestamp("created_at", nullable=True)
                .timestamp("updated_at", nullable=True)
                # -- Doris storage engine settings -----------------------
                .doris_unique_key(["peer_id", "message_id"])
                .doris_distributed_by("peer_id", buckets=32)
                .create_table()
            ),
            down=MigrationBuilder(
                "telegram_media_files", dialect="doris"
            ).drop_table(),
        ),
    ]
