"""Create ``web_media`` table for downloaded web media metadata.

Stores metadata for successfully downloaded web media files (images,
videos, documents, attachments) stored in RustFS.  Every successful
download creates a new row — re-downloading the same source_url
creates a separate row for audit trail.

Revision ID: 0015
"""
from __future__ import annotations

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

VERSION = "0015"


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="web_media_0001",
            description="Create web_media table for downloaded web media metadata",
            up=(
                MigrationBuilder("web_media")
                .uuid("id", primary_key=True, nullable=False)
                .text("source_url", nullable=False)
                .text("source_page_url", nullable=False)
                .uuid("seed_id", 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 --
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                # -- Indexes --
                .index(["source_url"])
                .index(["seed_id"])
                .index(["content_hash"])
                .create_table()
            ),
            down=MigrationBuilder("web_media").drop_table(),
        ),
    ]
