"""add_campaign_scheduling

Revision ID: b017scheduling
Revises: b016retests
Create Date: 2026-02-10 02:00:00.000000

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = "b017scheduling"
down_revision: Union[str, None] = "b016retests"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "campaigns",
        sa.Column("is_recurring", sa.Boolean, server_default="false", nullable=False),
    )
    op.add_column(
        "campaigns",
        sa.Column("recurrence_pattern", sa.String, nullable=True),
    )
    op.add_column(
        "campaigns",
        sa.Column("next_run_at", sa.DateTime, nullable=True),
    )
    op.add_column(
        "campaigns",
        sa.Column("last_run_at", sa.DateTime, nullable=True),
    )
    op.add_column(
        "campaigns",
        sa.Column(
            "parent_campaign_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("campaigns.id", ondelete="SET NULL"),
            nullable=True,
        ),
    )
    op.create_index("ix_campaigns_next_run", "campaigns", ["next_run_at"])
    op.create_index("ix_campaigns_parent", "campaigns", ["parent_campaign_id"])


def downgrade() -> None:
    op.drop_index("ix_campaigns_parent", table_name="campaigns")
    op.drop_index("ix_campaigns_next_run", table_name="campaigns")
    op.drop_column("campaigns", "parent_campaign_id")
    op.drop_column("campaigns", "last_run_at")
    op.drop_column("campaigns", "next_run_at")
    op.drop_column("campaigns", "recurrence_pattern")
    op.drop_column("campaigns", "is_recurring")
