"""add calendar description and slots

Revision ID: 20251106_152905_calendar_config
Revises: 215330c5ec15
Create Date: 2025-11-06T15:29:05.243479
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "20251106_152905_calendar_config"
down_revision = "215330c5ec15"
branch_labels = None
depends_on = None

def upgrade() -> None:
    with op.batch_alter_table("calendars") as batch_op:
        batch_op.add_column(sa.Column("description", sa.Text(), nullable=True))

    op.create_table(
        "calendar_slots",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("calendar_id", sa.Integer(), sa.ForeignKey("calendars.id", ondelete="CASCADE"), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),

        sa.Column("mon", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("tue", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("wed", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("thu", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("fri", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("sat", sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column("sun", sa.Boolean(), nullable=False, server_default=sa.false()),

        sa.Column("time_start", sa.Time(timezone=False), nullable=False),
        sa.Column("time_end", sa.Time(timezone=False), nullable=False),

        sa.Column("cost", sa.Numeric(10, 2), nullable=True),

        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
        sa.CheckConstraint("(time_end > time_start)", name="ck_calendar_slots_time_end_after_start"),
    )

    op.create_index("ix_calendar_slots_calendar_id", "calendar_slots", ["calendar_id"], unique=False)
    op.create_index("ix_calendar_slots_time_start", "calendar_slots", ["time_start"], unique=False)

    op.create_unique_constraint(
        "uq_calendar_slots_unique_band",
        "calendar_slots",
        ["calendar_id", "name"]
    )

def downgrade() -> None:
    op.drop_constraint("uq_calendar_slots_unique_band", "calendar_slots", type_="unique")
    op.drop_index("ix_calendar_slots_time_start", table_name="calendar_slots")
    op.drop_index("ix_calendar_slots_calendar_id", table_name="calendar_slots")
    op.drop_table("calendar_slots")

    with op.batch_alter_table("calendars") as batch_op:
        batch_op.drop_column("description")
