"""initial schema

Revision ID: 0001
Revises:
Create Date: 2026-02-24 00:00:00.000000
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

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


def upgrade() -> None:
    # Enums
    topic_status = postgresql.ENUM(
        "active", "paused", "archived", name="topicstatus", create_type=True
    )
    domain = postgresql.ENUM(
        "custodian", "railiance", "markitect", "coulomb_social", "personhood",
        "foerster_capabilities", name="domain", create_type=True
    )
    workstream_status = postgresql.ENUM(
        "active", "blocked", "completed", "archived", name="workstreamstatus", create_type=True
    )
    task_status = postgresql.ENUM(
        "wait", "todo", "progress", "done", "cancel", name="taskstatus", create_type=True
    )
    task_priority = postgresql.ENUM(
        "low", "medium", "high", "critical", name="taskpriority", create_type=True
    )
    decision_type = postgresql.ENUM(
        "made", "pending", name="decisiontype", create_type=True
    )
    decision_status = postgresql.ENUM(
        "open", "resolved", "escalated", "superseded", name="decisionstatus", create_type=True
    )

    # topics
    op.create_table(
        "topics",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column("slug", sa.String(100), nullable=False, unique=True),
        sa.Column("title", sa.String(255), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("domain", domain, nullable=False),
        sa.Column("status", topic_status, nullable=False, server_default="active"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
    )
    op.create_index("ix_topics_slug", "topics", ["slug"])

    # workstreams
    op.create_table(
        "workstreams",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "topic_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("topics.id", ondelete="RESTRICT"),
            nullable=False,
        ),
        sa.Column("slug", sa.String(100), nullable=False, unique=True),
        sa.Column("title", sa.String(255), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("status", workstream_status, nullable=False, server_default="active"),
        sa.Column("owner", sa.String(100), nullable=True),
        sa.Column("due_date", sa.Date, nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
    )
    op.create_index("ix_workstreams_slug", "workstreams", ["slug"])
    op.create_index("ix_workstreams_topic_id", "workstreams", ["topic_id"])

    # tasks
    op.create_table(
        "tasks",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "workstream_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("workstreams.id", ondelete="RESTRICT"),
            nullable=False,
        ),
        sa.Column("title", sa.String(255), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("status", task_status, nullable=False, server_default="todo"),
        sa.Column("priority", task_priority, nullable=False, server_default="medium"),
        sa.Column("assignee", sa.String(100), nullable=True),
        sa.Column("due_date", sa.Date, nullable=True),
        sa.Column("blocking_reason", sa.Text, nullable=True),
        sa.Column(
            "parent_task_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("tasks.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
    )
    op.create_index("ix_tasks_workstream_id", "tasks", ["workstream_id"])

    # decisions
    op.create_table(
        "decisions",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "topic_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("topics.id", ondelete="RESTRICT"),
            nullable=True,
        ),
        sa.Column(
            "workstream_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("workstreams.id", ondelete="RESTRICT"),
            nullable=True,
        ),
        sa.Column("title", sa.String(255), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("decision_type", decision_type, nullable=False, server_default="pending"),
        sa.Column("status", decision_status, nullable=False, server_default="open"),
        sa.Column("rationale", sa.Text, nullable=True),
        sa.Column("decided_by", sa.String(100), nullable=True),
        sa.Column("decided_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deadline", sa.DateTime(timezone=True), nullable=True),
        sa.Column("escalation_note", sa.Text, nullable=True),
        sa.Column(
            "superseded_by",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("decisions.id", ondelete="SET NULL"),
            nullable=True,
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.CheckConstraint(
            "topic_id IS NOT NULL OR workstream_id IS NOT NULL",
            name="ck_decisions_topic_or_workstream",
        ),
    )
    op.create_index("ix_decisions_topic_id", "decisions", ["topic_id"])
    op.create_index("ix_decisions_workstream_id", "decisions", ["workstream_id"])

    # progress_events
    op.create_table(
        "progress_events",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "topic_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("topics.id", ondelete="RESTRICT"),
            nullable=True,
        ),
        sa.Column(
            "workstream_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("workstreams.id", ondelete="RESTRICT"),
            nullable=True,
        ),
        sa.Column(
            "task_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("tasks.id", ondelete="RESTRICT"),
            nullable=True,
        ),
        sa.Column(
            "decision_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("decisions.id", ondelete="RESTRICT"),
            nullable=True,
        ),
        sa.Column("event_type", sa.String(50), nullable=False),
        sa.Column("summary", sa.Text, nullable=False),
        sa.Column("detail", postgresql.JSONB, nullable=True),
        sa.Column("author", sa.String(100), nullable=True),
        sa.Column("session_id", sa.String(100), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
    )
    op.create_index("ix_progress_events_topic_id", "progress_events", ["topic_id"])
    op.create_index("ix_progress_events_workstream_id", "progress_events", ["workstream_id"])
    op.create_index("ix_progress_events_task_id", "progress_events", ["task_id"])
    op.create_index("ix_progress_events_decision_id", "progress_events", ["decision_id"])
    op.create_index("ix_progress_events_event_type", "progress_events", ["event_type"])
    op.create_index("ix_progress_events_created_at", "progress_events", ["created_at"])


def downgrade() -> None:
    op.drop_table("progress_events")
    op.drop_table("decisions")
    op.drop_table("tasks")
    op.drop_table("workstreams")
    op.drop_table("topics")

    for name in [
        "topicstatus", "domain", "workstreamstatus",
        "taskstatus", "taskpriority", "decisiontype", "decisionstatus",
    ]:
        op.execute(f"DROP TYPE IF EXISTS {name}")
