"""Create the ``category`` table with self-referencing parent FK.

Revision ID: 0004
"""
from __future__ import annotations

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

VERSION = "0004"


def get_postgres_migrations() -> list[Migration]:
    return [
        Migration(
            version="category_0001",
            description="Create category table with self-referencing parent FK",
            up=(
                MigrationBuilder("category")
                .bigint("id", primary_key=True, nullable=False)
                .varchar("title", 100, nullable=False)
                .varchar("name", 100, nullable=False)
                .bigint("parent_id", nullable=True)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["title"], unique=True)
                .index(["parent_id"])
                .foreign_key("parent_id", "category", ref_column="id", on_delete="SET NULL")
                .create_table()
            ),
            down=MigrationBuilder("category").drop_table(),
        ),
    ]
