"""Create codal_crawler tables for statement listings, details, errors, and files.

Revision ID: 0031
"""

from __future__ import annotations

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

VERSION = "0031"


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="codal_listings_0001",
            description="Create codal_statement_listings table",
            up=(
                MigrationBuilder("codal_statement_listings")
                .uuid("id", primary_key=True, nullable=False)
                .text("statement_type", nullable=False)
                .integer("page", nullable=False, default=1)
                .text("raw_html", nullable=True)
                .timestamp("crawl_time", nullable=True)
                .jsonb("statements", nullable=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["statement_type"])
                .create_table()
            ),
            down=MigrationBuilder("codal_statement_listings").drop_table(),
        ),
        Migration(
            version="codal_details_0001",
            description="Create codal_statement_details table",
            up=(
                MigrationBuilder("codal_statement_details")
                .uuid("id", primary_key=True, nullable=False)
                .text("statement_id", nullable=False)
                .text("tab_slug", nullable=False)
                .text("tab_name", nullable=True)
                .text("tab_url", nullable=True)
                .text("raw_html", nullable=True)
                .jsonb("attachments", nullable=True)
                .timestamp("crawl_time", nullable=True)
                .timestamp("parse_time", nullable=True)
                .text("status", nullable=True)
                .text("error_message", nullable=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["statement_id"])
                .index(["status"])
                .create_table()
            ),
            down=MigrationBuilder("codal_statement_details").drop_table(),
        ),
        Migration(
            version="codal_errors_0001",
            description="Create codal_errors table for crawl error logging",
            up=(
                MigrationBuilder("codal_errors")
                .uuid("id", primary_key=True, nullable=False)
                .text("url", nullable=False)
                .text("statement_id", nullable=True)
                .bigint("publisher_id", nullable=True)
                .integer("status_code", nullable=True)
                .text("error_message", nullable=True)
                .text("error_type", nullable=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["statement_id"])
                .index(["error_type"])
                .index(["created_at"])
                .create_table()
            ),
            down=MigrationBuilder("codal_errors").drop_table(),
        ),
        Migration(
            version="codal_files_0001",
            description="Create codal_files table",
            up=(
                MigrationBuilder("codal_files")
                .uuid("id", primary_key=True, nullable=False)
                .text("statement_id", nullable=False)
                .text("filename", nullable=False)
                .text("url", nullable=True)
                .text("content_type", nullable=True)
                .bigint("size_bytes", nullable=True)
                .text("sha256", nullable=True)
                .text("rustfs_bucket", nullable=True)
                .text("rustfs_key", nullable=True)
                .text("download_status", nullable=False, default="pending")
                .text("download_error", nullable=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["statement_id"])
                .create_table()
            ),
            down=MigrationBuilder("codal_files").drop_table(),
        ),
    ]
