"""Add publisher metadata, statement detail, and fetch lifecycle columns
to ``codal_statements``.  Revision ID: 0020 (merged 0019 + 0020).
"""

from __future__ import annotations

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

VERSION = "0020"


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="codal_statements_0004",
            description=(
                "Add publish_date, code, statement_id, statement_title, "
                "sent_datetime, discovery_time, fetch_datetime, status, "
                "tracking_number, view_number, alert columns to codal_statements"
            ),
            up=(
                MigrationBuilder("codal_statements")
                .add_column("publish_date", "TIMESTAMPTZ", nullable=True)
                .add_column("code", "TEXT", nullable=True)
                .add_column("statement_id", "TEXT", nullable=True)
                .add_column("statement_title", "TEXT", nullable=True)
                .add_column("sent_datetime", "TEXT", nullable=True)
                .add_column("discovery_time", "TIMESTAMPTZ", nullable=True)
                .add_column("fetch_datetime", "TIMESTAMPTZ", nullable=True)
                .add_column("status", "TEXT", nullable=True)
                .add_column("tracking_number", "TEXT", nullable=True)
                .add_column("view_number", "TEXT", nullable=True)
                .add_column("alert", "TEXT", nullable=True)
                .add_index(["statement_id"])
                .add_index(["status"])
                .add_index(["publisher_id", "statement_type"])
                .alter_table()
            ),
            down=(
                MigrationBuilder("codal_statements")
                .drop_column("publish_date")
                .drop_column("code")
                .drop_column("statement_id")
                .drop_column("statement_title")
                .drop_column("sent_datetime")
                .drop_column("discovery_time")
                .drop_column("fetch_datetime")
                .drop_column("status")
                .drop_column("tracking_number")
                .drop_column("view_number")
                .drop_column("alert")
                .alter_table()
            ),
        ),
    ]
