"""Seed case-lifecycle: staff-managed case records with an enforced status graph.

Revision ID: 133
Revises: 132
"""
from datetime import datetime, timezone
from uuid import uuid4

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

revision = "133"
down_revision = "132"
branch_labels = None
depends_on = None

feature_packs = sa.table(
    "feature_packs",
    sa.column("id", postgresql.UUID(as_uuid=True)),
    sa.column("slug", sa.String),
    sa.column("name", sa.String),
    sa.column("description", sa.Text),
    sa.column("version", sa.String),
    sa.column("tags", postgresql.JSONB),
    sa.column("status", sa.String),
    sa.column("composition", postgresql.JSONB),
    sa.column("input_schema", postgresql.JSONB),
    sa.column("access_policy", postgresql.JSONB),
)

_CASE_ENTITY = {
    "entity_key": "case_record",
    "name": "Case",
    "plural_name": "Cases",
    "schema": {
        "fields": [
            {"key": "title", "name": "Title", "type": "text", "required": True},
            {"key": "reference", "name": "Reference", "type": "text", "indexed": True},
            {"key": "applicant", "name": "Applicant", "type": "text", "indexed": True},
            {"key": "email", "name": "Email", "type": "text", "indexed": True},
            {"key": "notes", "name": "Notes", "type": "text"},
            {
                "key": "status",
                "name": "Status",
                "type": "select",
                "required": True,
                "indexed": True,
                "options": ["open", "in_review", "waiting", "resolved", "closed"],
            },
        ],
        "workflow": {
            "field": "status",
            "transitions": {
                "open": ["in_review", "closed"],
                "in_review": ["waiting", "resolved", "closed"],
                "waiting": ["in_review", "closed"],
                "resolved": ["closed"],
                "closed": [],
            },
        },
    },
}


def upgrade() -> None:
    now = datetime.now(timezone.utc)
    op.bulk_insert(
        feature_packs,
        [
            {
                "id": uuid4(),
                "slug": "case-lifecycle",
                "name": "Case Lifecycle",
                "description": (
                    "Staff-managed case or application files with a durable record and "
                    "an enforced status graph — not a project-local tracker."
                ),
                "version": "1.0.0",
                "tags": ["cases", "workflow", "applications"],
                "status": "active",
                "composition": {
                    "function_slugs": [],
                    "entities": [_CASE_ENTITY],
                    "connector_actions": [],
                    "requires": ["workflow.transition"],
                },
                "input_schema": {"type": "object", "properties": {}},
                "access_policy": {"minimum_plan": "free"},
                "created_at": now,
                "updated_at": now,
            },
        ],
    )


def downgrade() -> None:
    op.execute("DELETE FROM feature_packs WHERE slug = 'case-lifecycle'")
