"""Wire reminder scheduling into appointment-booking's function deploy.

The library functions were already rewritten (elsewhere) onto the real Cloud
booking authority (bookings.ts) and reminder_channel already existed in this
pack's input_schema, but nothing actually threaded that config into the
deployed booking-create function's env -- reminders were never wired end to
end. Adds reminder_lead_minutes/reminder_connection_id and resolves all three
into booking-create's static_env_vars via the $config. mechanism
(PackInstallService.install_pack / EdgeFunctionsService._push_function), same
pattern used for contact-capture's notify_to_email.

Revision ID: 108
Revises: 107
"""
from datetime import datetime, timezone

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

revision = "108"
down_revision = "107"
branch_labels = None
depends_on = None

feature_packs = sa.table(
    "feature_packs",
    sa.column("slug", sa.String),
    sa.column("version", sa.String),
    sa.column("composition", postgresql.JSONB),
    sa.column("input_schema", postgresql.JSONB),
    sa.column("updated_at", sa.DateTime(timezone=True)),
)

_COMPOSITION = {
    "entities": [],
    "requires": [],
    "connector_actions": ["calendly.create_scheduling_link", "google_calendar.create_event"],
    "function_slugs": [
        {
            "library_slug": "booking-create",
            "project_slug": "booking-create",
            "trigger_config": {
                "static_env_vars": {
                    "REMINDER_CHANNEL": "$config.reminder_channel",
                    "REMINDER_LEAD_MINUTES": "$config.reminder_lead_minutes",
                    "REMINDER_CONNECTION_ID": "$config.reminder_connection_id",
                },
            },
        },
        "booking-cancel",
        "booking-list-availability",
    ],
}

_INPUT_SCHEMA = {
    "type": "object",
    "properties": {
        "calendar_sync": {"type": "boolean", "default": False},
        "reminder_channel": {"type": "string", "enum": ["email", "sms"], "default": "email"},
        "reminder_lead_minutes": {
            "type": "number",
            "default": 1440,
            "description": "Minutes before the appointment to send the reminder. Default 1440 (24h).",
        },
        "reminder_connection_id": {
            "type": "string",
            "description": "Required only when reminder_channel is \"sms\" — a configured Twilio connection id.",
        },
    },
}


def upgrade() -> None:
    op.execute(
        sa.update(feature_packs)
        .where(feature_packs.c.slug == "appointment-booking")
        .values(
            version="1.1.0",
            composition=_COMPOSITION,
            input_schema=_INPUT_SCHEMA,
            updated_at=datetime.now(timezone.utc),
        )
    )


def downgrade() -> None:
    op.execute(
        sa.update(feature_packs)
        .where(feature_packs.c.slug == "appointment-booking")
        .values(
            version="1.0.0",
            composition={
                "entities": [],
                "requires": [],
                "function_slugs": ["booking-create", "booking-cancel", "booking-list-availability"],
                "connector_actions": ["calendly.create_scheduling_link", "google_calendar.create_event"],
            },
            input_schema={
                "type": "object",
                "properties": {
                    "calendar_sync": {"type": "boolean", "default": False},
                    "reminder_channel": {"type": "string", "enum": ["email", "sms"], "default": "email"},
                },
            },
        )
    )
