"""scope school holidays to academic periods

Revision ID: f3c4d5e6a7b8
Revises: dd100f3958dc
Create Date: 2026-03-31 12:20:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'f3c4d5e6a7b8'
down_revision: Union[str, None] = 'dd100f3958dc'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column('school_holidays', sa.Column('academic_period_id', sa.Integer(), nullable=True))
    op.create_index(
        op.f('ix_school_holidays_academic_period_id'),
        'school_holidays',
        ['academic_period_id'],
        unique=False,
    )
    op.create_foreign_key(
        'fk_school_holidays_academic_period_id',
        'school_holidays',
        'academic_periods',
        ['academic_period_id'],
        ['id'],
        ondelete='SET NULL',
    )
    op.drop_constraint('uq_school_holidays_unique', 'school_holidays', type_='unique')
    op.create_unique_constraint(
        'uq_school_holidays_unique',
        'school_holidays',
        ['name', 'start_date', 'end_date', 'region', 'academic_period_id'],
    )


def downgrade() -> None:
    op.drop_constraint('uq_school_holidays_unique', 'school_holidays', type_='unique')
    op.create_unique_constraint(
        'uq_school_holidays_unique',
        'school_holidays',
        ['name', 'start_date', 'end_date', 'region'],
    )
    op.drop_constraint('fk_school_holidays_academic_period_id', 'school_holidays', type_='foreignkey')
    op.drop_index(op.f('ix_school_holidays_academic_period_id'), table_name='school_holidays')
    op.drop_column('school_holidays', 'academic_period_id')