"""Architecture Lane Workforce (freeze lifted for the Native Capability Baseline):
worker directory records with role, skills, and a credential expiry date.
Shift assignment is a separate Cloud-native primitive, not a field here.

Revision ID: 157
Revises: 156
"""
from uuid import uuid4

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB, UUID, insert

revision = "157"
down_revision = "156"
branch_labels = None
depends_on = None

feature_packs = sa.table("feature_packs",
    sa.column("id", 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", JSONB),
    sa.column("status", sa.String),
    sa.column("composition", JSONB),
    sa.column("input_schema", JSONB),
    sa.column("access_policy", JSONB),
)


def workforce_composition() -> dict:
    from src.domain.feature_packs.crm_authorization import staff_policy
    from src.domain.feature_packs.normalize import stamp_composition

    return stamp_composition("workforce-directory", {
        "function_slugs": [], "connector_actions": [], "requires": [],
        "entities": [{
            "entity_key": "worker_record", "name": "Worker", "plural_name": "Workers",
            "rls_enabled": False, "owner_managed": False,
            "schema": {
                "fields": [
                    {
                        "key": "name", "name": "Name", "type": "text",
                        "required": True, "indexed": True,
                    },
                    {"key": "email", "name": "Email", "type": "text", "indexed": True},
                    {"key": "phone", "name": "Phone", "type": "text"},
                    {"key": "role", "name": "Role", "type": "text"},
                    {"key": "skills", "name": "Skills", "type": "text"},
                    {"key": "credential_name", "name": "Credential", "type": "text"},
                    {"key": "credential_expiry_date", "name": "Credential expiry", "type": "date"},
                    {"key": "status", "name": "Status", "type": "select", "required": True,
                     "indexed": True, "options": ["active", "inactive"]},
                ],
                "access_policy": staff_policy(),
            },
        }],
    }, implementation_version="1.0.0")


def upgrade() -> None:
    op.get_bind().execute(insert(feature_packs).values(
        id=uuid4(), slug="workforce-directory", name="Workforce Directory", version="1.0.0",
        description=(
            "Staff-managed worker directory records with role, skills, and a "
            "credential expiry date. Shift assignment is a separate Cloud-native "
            "primitive (workforce.shift_assign / workforce.shift_release), not a "
            "field here -- no payroll, timesheet, or clock-in engine."
        ),
        tags=["workforce"], status="active", composition=workforce_composition(),
        input_schema={"type": "object", "properties": {}}, access_policy={"minimum_plan": "free"},
    ).on_conflict_do_nothing(index_elements=["slug"]))


def downgrade() -> None:
    pass
