"""PostgreSQL migration: create telegram_media_files table.

Stores metadata for successfully downloaded Telegram media files
(photo, video, document, gif, audio, sticker) with S3 storage references.
"""

from __future__ import annotations

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

VERSION = "0002"


def get_postgres_migrations() -> list[Migration]:
    """Return the CREATE TABLE migration for telegram_media_files."""
    return [
        Migration(
            version="tg_media_file_0001",
            description="Create telegram_media_files table for downloaded media metadata",
            up=(
                MigrationBuilder("telegram_media_files")
                # -- 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)
                .text("storage_url", nullable=True)
                # -- 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_short("duration_seconds", nullable=True)
                # -- Timestamps ------------------------------------------
                .timestamp("downloaded_at", nullable=False)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                # -- Indexes ---------------------------------------------
                .index(["peer_id", "message_id"], unique=True)
                .index(["media_type"])
                .index(["downloaded_at"])
                .index(["peer_id"])
                .create_table()
            ),
            down=MigrationBuilder("telegram_media_files").drop_table(),
        ),
    ]
