"""Create the ``follows`` table with a unique (user, target_type, target_id) constraint.

Revision ID: 0002
"""
from __future__ import annotations

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

VERSION = "0002"


def get_postgres_migrations() -> list[Migration]:
    """Return all PostgreSQL migrations defined in this version file."""
    return [
        Migration(
            version="follow_0001",
            description="Create follows table for polymorphic user follows",
            up=(
                MigrationBuilder("follows")
                .bigint("id", primary_key=True, nullable=False)
                .bigint("user_id", nullable=False)
                .varchar("target_type", 20, nullable=False)
                .varchar("target_id", 128, nullable=False)
                .timestamp("created_at", nullable=True, default="now()")
                .timestamp("updated_at", nullable=True, default="now()")
                .index(["user_id", "target_type", "target_id"], unique=True)
                .index(["target_type", "target_id"])
                .index(["user_id"])
                # .foreign_key("user_id", "users")
                .create_table()
            ),
            down=MigrationBuilder("follows").drop_table(),
        ),
    ]
