"""Voice / AI call-center policy tables.

Revision ID: 088
Revises: 087
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql


revision = "088"
down_revision = "087"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "project_voice_config",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("max_concurrent_calls", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("default_agent_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("project_id"),
    )
    op.create_table(
        "project_voice_agents",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.String(length=128), nullable=False),
        sa.Column("version", sa.String(length=32), nullable=False, server_default="1"),
        sa.Column("provider", sa.String(length=32), nullable=False, server_default="openai_realtime"),
        sa.Column("model", sa.String(length=128), nullable=True),
        sa.Column("voice", sa.String(length=64), nullable=True),
        sa.Column("instructions", sa.Text(), nullable=True),
        sa.Column("tools_json", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("transfer_targets_json", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("secret_ref", sa.String(length=128), nullable=True),
        sa.Column("status", sa.String(length=20), nullable=False, server_default="active"),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index("ix_project_voice_agents_project_id", "project_voice_agents", ["project_id"])
    op.create_table(
        "project_phone_numbers",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("e164", sa.String(length=32), nullable=False),
        sa.Column("provider", sa.String(length=32), nullable=False, server_default="twilio"),
        sa.Column("provider_sid", sa.String(length=64), nullable=True),
        sa.Column("inbound_agent_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("outbound_agent_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("status", sa.String(length=20), nullable=False, server_default="active"),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("project_id", "e164", name="uq_project_phone_e164"),
    )
    op.create_index("ix_project_phone_numbers_project_id", "project_phone_numbers", ["project_id"])


def downgrade() -> None:
    op.drop_index("ix_project_phone_numbers_project_id", table_name="project_phone_numbers")
    op.drop_table("project_phone_numbers")
    op.drop_index("ix_project_voice_agents_project_id", table_name="project_voice_agents")
    op.drop_table("project_voice_agents")
    op.drop_table("project_voice_config")
