"""Add trusted external system definitions and per-user connections.

Revision ID: 0026
Revises: 0025
Create Date: 2026-08-04
"""
from typing import Sequence, Union

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


revision: str = "0026"
down_revision: Union[str, None] = "0025"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "external_system_definitions",
        sa.Column("definition_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("provider", sa.Text(), nullable=False),
        sa.Column("name", sa.Text(), nullable=False),
        sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("access_mode", sa.Text(), server_default="selected", nullable=False),
        sa.Column("enabled", sa.Boolean(), server_default="true", nullable=False),
        sa.Column("created_by", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["created_by"], ["users.user_id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("definition_id"),
        sa.UniqueConstraint(
            "provider", "name", name="uq_external_system_definition_provider_name"
        ),
    )
    op.create_table(
        "external_systems",
        sa.Column("external_system_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("definition_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("provider", sa.Text(), nullable=False),
        sa.Column("connector", sa.Text(), server_default="openapi", nullable=False),
        sa.Column("name", sa.Text(), nullable=False),
        sa.Column("credentials", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("config", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("status", sa.Text(), server_default="active", nullable=False),
        sa.Column("last_verified_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.user_id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(
            ["definition_id"],
            ["external_system_definitions.definition_id"],
            ondelete="RESTRICT",
        ),
        sa.PrimaryKeyConstraint("external_system_id"),
        sa.UniqueConstraint(
            "user_id", "definition_id", name="uq_external_system_user_definition"
        ),
    )
    op.create_index(
        "ix_external_systems_user_status",
        "external_systems",
        ["user_id", "status"],
    )
def downgrade() -> None:
    op.drop_index("ix_external_systems_user_status", table_name="external_systems")
    op.drop_table("external_systems")
    op.drop_table("external_system_definitions")
