"""Add invariants table.

Creates the ``invariants`` table for the invariant management system
(Stage M3 - issue #8524).  Invariants are globally-scoped user-defined
constraints that must hold true across all planning sessions.

Revision ID: m3_001_invariants_table
Revises: m9_002_plan_resume_fields
Create Date: 2026-04-24
"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "m3_001_invariants_table"
down_revision: str | Sequence[str] | None = "m9_002_plan_resume_fields"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Create the invariants table with index on is_active."""
    op.create_table(
        "invariants",
        sa.Column("id", sa.String(36), nullable=False),
        sa.Column("description", sa.Text, nullable=False),
        sa.Column("created_at", sa.String(30), nullable=False),
        sa.Column(
            "is_active",
            sa.Boolean,
            nullable=False,
            server_default=sa.text("1"),
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_invariants_is_active", "invariants", ["is_active"])


def downgrade() -> None:
    """Drop the invariants table."""
    op.drop_index("ix_invariants_is_active", table_name="invariants")
    op.drop_table("invariants")
