"""Add v2 of trigger schemas [3855b5d051cd].

Revision ID: 3855b5d051cd
Revises: 97109a4a8d26
Create Date: 2026-01-28 13:07:51.002374

"""

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision = "3855b5d051cd"
down_revision = "97109a4a8d26"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Upgrade database schema and/or data, creating a new revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "trigger",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("is_archived", sa.Boolean(), nullable=False),
        sa.Column("type", sa.VARCHAR(length=20), nullable=False),
        sa.Column("flavor", sa.VARCHAR(length=50), nullable=False),
        sa.Column(
            "configuration",
            sa.TEXT(),
            nullable=False,
        ),
        sa.Column("project_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("user_id", sqlmodel.sql.sqltypes.GUID(), nullable=True),
        sa.Column("next_occurrence", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["project.id"],
            name="fk_trigger_project_id_project",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name="fk_trigger_user_id_user",
            ondelete="SET NULL",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "name", "project_id", name="unique_trigger_name_in_project"
        ),
    )
    with op.batch_alter_table("trigger", schema=None) as batch_op:
        batch_op.create_index(
            "ix_trigger_flavor_next_occurrence",
            ["flavor", "next_occurrence"],
            unique=False,
        )
        batch_op.create_index("ix_trigger_type", ["type"], unique=False)

    op.create_table(
        "trigger_snapshot",
        sa.Column("trigger_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("snapshot_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["snapshot_id"],
            ["pipeline_snapshot.id"],
            ondelete="CASCADE",
            name="fk_trigger_snapshot_snapshot_id_pipeline_snapshot",
        ),
        sa.ForeignKeyConstraint(
            ["trigger_id"],
            ["trigger.id"],
            ondelete="CASCADE",
            name="fk_trigger_snapshot_trigger_id_trigger",
        ),
        sa.PrimaryKeyConstraint("trigger_id", "snapshot_id"),
        sa.UniqueConstraint(
            "trigger_id", "snapshot_id", name="unique_trigger_snapshot_link"
        ),
    )
    op.create_table(
        "trigger_execution",
        sa.Column("trigger_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column(
            "pipeline_run_id", sqlmodel.sql.sqltypes.GUID(), nullable=False
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["pipeline_run_id"],
            ["pipeline_run.id"],
            ondelete="CASCADE",
            name="fk_trigger_execution_pipeline_run_id_pipeline_run",
        ),
        sa.ForeignKeyConstraint(
            ["trigger_id"],
            ["trigger.id"],
            ondelete="CASCADE",
            name="fk_trigger_execution_trigger_id_trigger",
        ),
        sa.PrimaryKeyConstraint("trigger_id", "pipeline_run_id"),
        sa.UniqueConstraint(
            "trigger_id", "pipeline_run_id", name="unique_trigger_execution"
        ),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade database schema and/or data back to the previous revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("trigger_execution")
    op.drop_table("trigger_snapshot")
    with op.batch_alter_table("trigger", schema=None) as batch_op:
        batch_op.drop_index("ix_trigger_type")
        batch_op.drop_index("ix_trigger_flavor_next_occurrence")

    op.drop_table("trigger")
    # ### end Alembic commands ###
