"""Phase 8: system_configs table for runtime configuration.

Revision ID: b033syscfg
Revises: b032phase7
"""
from typing import Sequence, Union
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from alembic import op

revision: str = "b033syscfg"
down_revision: Union[str, None] = "b032phase7"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def _table_exists(name: str) -> bool:
    bind = op.get_bind()
    insp = sa.inspect(bind)
    return name in insp.get_table_names()


def upgrade() -> None:
    if not _table_exists("system_configs"):
        op.create_table(
            "system_configs",
            sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
            sa.Column("key", sa.String(200), unique=True, nullable=False),
            sa.Column("value", sa.Text, nullable=True),
            sa.Column("description", sa.String(500), nullable=True),
            sa.Column(
                "updated_at",
                sa.DateTime(timezone=True),
                server_default=sa.text("now()"),
            ),
        )
        op.create_index("ix_system_configs_key", "system_configs", ["key"])


def downgrade() -> None:
    if _table_exists("system_configs"):
        op.drop_index("ix_system_configs_key", table_name="system_configs")
        op.drop_table("system_configs")
