"""Seed lead-lifecycle: staff-managed lead entity with an enforced status graph.

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

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

revision = "132"
down_revision = "131"
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),
)

_LEAD_ENTITY = {
    "entity_key": "lead",
    "name": "Lead",
    "plural_name": "Leads",
    "schema": {
        "fields": [
            {"key": "name", "name": "Name", "type": "text", "required": True},
            {"key": "email", "name": "Email", "type": "text", "required": True, "indexed": True},
            {"key": "phone", "name": "Phone", "type": "text"},
            {"key": "source", "name": "Source", "type": "text", "indexed": True},
            {"key": "notes", "name": "Notes", "type": "text"},
            {
                "key": "status",
                "name": "Status",
                "type": "select",
                "required": True,
                "indexed": True,
                "options": ["new", "contacted", "qualified", "won", "lost"],
            },
        ],
        "workflow": {
            "field": "status",
            "transitions": {
                "new": ["contacted", "lost"],
                "contacted": ["qualified", "lost"],
                "qualified": ["won", "lost"],
                "won": [],
                "lost": [],
            },
        },
    },
}


def upgrade() -> None:
    now = datetime.now(timezone.utc)
    op.bulk_insert(
        feature_packs,
        [
            {
                "id": uuid4(),
                "slug": "lead-lifecycle",
                "name": "Lead Lifecycle",
                "description": (
                    "Staff-managed sales leads with a durable record and an enforced "
                    "status graph — not a HubSpot substitute or a project-local store."
                ),
                "version": "1.0.0",
                "tags": ["crm", "leads", "pipeline"],
                "status": "active",
                "composition": {
                    "function_slugs": [],
                    "entities": [_LEAD_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 = 'lead-lifecycle'")
