"""Create ``web_parsed_pages`` table for web-parser output.

Stores parsed web page content — title, description, author, content,
images, links, tables, schema data, and operational metadata.

Upsert target is ``url`` (unique constraint).  Every re-parse of the same
URL replaces the existing row.

Revision ID: 0013
"""
from __future__ import annotations

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

VERSION = "0013"


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="parsed_page_0001",
            description="Create parsed_pages table for web-parser output",
            up=(
                MigrationBuilder("web_parsed_pages")
                .uuid("id", primary_key=True, nullable=False)
                .text("url", nullable=False)
                .uuid("seed_id", 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 --
                .timestamp("parsed_at", nullable=True)
                .jsonb("diagnostics", nullable=True)
                # -- Timestamps --
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                # -- Indexes --
                .index(["url"], unique=True)
                .index(["seed_id"])
                .index(["parsed_at"])
                .create_table()
            ),
            down=MigrationBuilder("web_parsed_pages").drop_table(),
        ),
    ]
