from alembic import op
import sqlalchemy as sa

revision = "0007_add_oid_entries"
down_revision = "0006_update_calendar_entries"
branch_labels = None
depends_on = None


def upgrade():
    # Add order_id column
    op.add_column(
        "calendar_entries",
        sa.Column("order_id", sa.Integer(), nullable=True),
    )
    op.create_foreign_key(
        "fk_calendar_entries_order_id",
        "calendar_entries",
        "orders",
        ["order_id"],
        ["id"],
        ondelete="SET NULL",
    )
    op.create_index(
        "ix_calendar_entries_order_id",
        "calendar_entries",
        ["order_id"],
        unique=False,
    )

    # Optional: add an index on state if you want faster queries by state
    op.create_index(
        "ix_calendar_entries_state",
        "calendar_entries",
        ["state"],
        unique=False,
    )


def downgrade():
    # Drop indexes and FK in reverse order
    op.drop_index("ix_calendar_entries_state", table_name="calendar_entries")

    op.drop_index("ix_calendar_entries_order_id", table_name="calendar_entries")
    op.drop_constraint(
        "fk_calendar_entries_order_id",
        "calendar_entries",
        type_="foreignkey",
    )
    op.drop_column("calendar_entries", "order_id")
