"""added campaign table

Revision ID: 08bb6e7f1397
Revises: 0c1223cc266f
Create Date: 2025-07-23 18:46:38.955381

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "08bb6e7f1397"
down_revision: Union[str, None] = "0c1223cc266f"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "campaigns",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("organization_id", sa.Integer(), nullable=False),
        sa.Column("workflow_id", sa.Integer(), nullable=False),
        sa.Column("created_by", sa.Integer(), nullable=False),
        sa.Column("source_type", sa.String(), nullable=False),
        sa.Column("source_id", sa.String(), nullable=False),
        sa.Column(
            "state",
            sa.Enum(
                "created",
                "running",
                "paused",
                "completed",
                "failed",
                name="campaign_state",
            ),
            nullable=False,
        ),
        sa.Column("total_rows", sa.Integer(), nullable=True),
        sa.Column("processed_rows", sa.Integer(), nullable=False),
        sa.Column("failed_rows", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["created_by"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
        ),
        sa.ForeignKeyConstraint(
            ["workflow_id"],
            ["workflows.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_campaigns_id"), "campaigns", ["id"], unique=False)
    op.create_index(op.f("ix_campaigns_name"), "campaigns", ["name"], unique=False)
    op.create_index(
        "ix_campaigns_org_id", "campaigns", ["organization_id"], unique=False
    )
    op.create_index("ix_campaigns_state", "campaigns", ["state"], unique=False)
    op.create_index(
        "ix_campaigns_workflow_id", "campaigns", ["workflow_id"], unique=False
    )
    op.add_column(
        "workflow_runs", sa.Column("campaign_id", sa.Integer(), nullable=True)
    )
    op.create_foreign_key(None, "workflow_runs", "campaigns", ["campaign_id"], ["id"])
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "workflow_runs", type_="foreignkey")
    op.drop_column("workflow_runs", "campaign_id")
    op.drop_index("ix_campaigns_workflow_id", table_name="campaigns")
    op.drop_index("ix_campaigns_state", table_name="campaigns")
    op.drop_index("ix_campaigns_org_id", table_name="campaigns")
    op.drop_index(op.f("ix_campaigns_name"), table_name="campaigns")
    op.drop_index(op.f("ix_campaigns_id"), table_name="campaigns")
    op.drop_table("campaigns")
    # ### end Alembic commands ###
