"""add_source_connection_table

Revision ID: 210e475985ae
Revises: 1e286802b4d3
Create Date: 2025-05-05 19:19:31.956506

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy.exc import ProgrammingError


# revision identifiers, used by Alembic.
revision = "210e475985ae"

down_revision = "1e286802b4d3"
branch_labels = None
depends_on = None


def upgrade():

    # Now create the tables
    op.create_table(
        "collection",
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("readable_id", sa.String(), nullable=False),
        sa.Column(
            "status",
            sa.Enum("ACTIVE", "NEEDS_SOURCE", "ERROR", name="collectionstatus"),
            nullable=False,
        ),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("modified_at", sa.DateTime(), nullable=False),
        sa.Column("created_by_email", sa.String(), nullable=False),
        sa.Column("modified_by_email", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["created_by_email"],
            ["user.email"],
        ),
        sa.ForeignKeyConstraint(
            ["modified_by_email"],
            ["user.email"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("readable_id"),
    )

    op.create_table(
        "source_connection",
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("short_name", sa.String(), nullable=False),
        sa.Column("config_fields", sa.JSON(), nullable=True),
        sa.Column("dag_id", sa.UUID(), nullable=True),
        sa.Column("sync_id", sa.UUID(), nullable=True),
        sa.Column("integration_credential_id", sa.UUID(), nullable=True),
        sa.Column("readable_collection_id", sa.String(), nullable=True),
        sa.Column(
            "status",
            sa.Enum("ACTIVE", "INACTIVE", "ERROR", name="sourceconnectionstatus"),
            nullable=False,
        ),
        sa.Column("cron_schedule", sa.String(), nullable=True),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("modified_at", sa.DateTime(), nullable=False),
        sa.Column("created_by_email", sa.String(), nullable=False),
        sa.Column("modified_by_email", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["readable_collection_id"], ["collection.readable_id"], ondelete="SET NULL"
        ),
        sa.ForeignKeyConstraint(
            ["created_by_email"],
            ["user.email"],
        ),
        sa.ForeignKeyConstraint(["dag_id"], ["sync_dag.id"], ondelete="SET NULL"),
        sa.ForeignKeyConstraint(
            ["integration_credential_id"], ["integration_credential.id"], ondelete="SET NULL"
        ),
        sa.ForeignKeyConstraint(
            ["modified_by_email"],
            ["user.email"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
        ),
        sa.ForeignKeyConstraint(["sync_id"], ["sync.id"], ondelete="SET NULL"),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("source_connection")
    op.drop_table("collection")
    op.execute("DROP TYPE IF EXISTS sourceconnectionstatus")
    op.execute("DROP TYPE IF EXISTS collectionstatus")
    # ### end Alembic commands ###
