"""Architecture Lane Contract (freeze lifted for the Native Capability Baseline):
contract/agreement lifecycle records with parties and dates. Signature
authority stays external/optional -- never a hard requirement.

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

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

revision = "156"
down_revision = "155"
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 contract_composition() -> dict:
    from src.domain.feature_packs.crm_authorization import staff_policy
    from src.domain.feature_packs.normalize import stamp_composition

    return stamp_composition("contract-management", {
        "function_slugs": [], "connector_actions": [], "requires": [],
        "entities": [{
            "entity_key": "contract", "name": "Contract", "plural_name": "Contracts",
            "rls_enabled": False, "owner_managed": False,
            "schema": {
                "fields": [
                    {
                        "key": "reference", "name": "Reference", "type": "text",
                        "required": True, "indexed": True,
                    },
                    {
                        "key": "counterparty_name", "name": "Counterparty", "type": "text",
                        "required": True,
                    },
                    {
                        "key": "counterparty_email", "name": "Counterparty email", "type": "text",
                        "indexed": True,
                    },
                    {"key": "start_date", "name": "Start date", "type": "date"},
                    {"key": "end_date", "name": "End date", "type": "date"},
                    {"key": "renewal_date", "name": "Renewal date", "type": "date"},
                    {"key": "notice_date", "name": "Notice date", "type": "date"},
                    {"key": "notes", "name": "Notes", "type": "text"},
                    {"key": "status", "name": "Status", "type": "select", "required": True,
                     "indexed": True, "options": ["draft", "active", "terminated", "renewed"]},
                ],
                "access_policy": staff_policy(),
            },
        }],
    }, implementation_version="1.0.0")


def upgrade() -> None:
    op.get_bind().execute(insert(feature_packs).values(
        id=uuid4(), slug="contract-management", name="Contract Management", version="1.0.0",
        description=(
            "Staff-managed contract/agreement lifecycle records with parties and "
            "start/end/renewal/notice dates. E-signature stays an external, "
            "optional integration -- never a hard requirement."
        ),
        tags=["contract"], status="active", composition=contract_composition(),
        input_schema={"type": "object", "properties": {}}, access_policy={"minimum_plan": "free"},
    ).on_conflict_do_nothing(index_elements=["slug"]))


def downgrade() -> None:
    pass
