"""add branch operating hours

Revision ID: ad2d93bd9512
Revises: a094fcc9e4cb
Create Date: 2026-07-21 15:36:19.194723

"""

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 = "ad2d93bd9512"
down_revision: Union[str, None] = "a094fcc9e4cb"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # NOTE: autogenerate also proposed op.drop_table('spatial_ref_sys')
    # (a PostGIS system table — dropping it would break PostGIS
    # entirely) and dropping refresh_tokens_token_hash_key (recurring,
    # pre-existing unique-index-vs-named-constraint drift unrelated to
    # this migration — same drift called out in 42828b3f2eea). Both
    # removed by hand. working_days gets a server_default of every
    # weekday so existing branches backfill to "every day" (a no-op
    # restriction, matching their behavior before this migration —
    # opens_at/closes_at stay null so they're not hour-restricted at
    # all yet), same pattern as d51c0018aa05's own working_days column
    # for CourierProfile.
    op.add_column("branches", sa.Column("opens_at", sa.Time(), nullable=True))
    op.add_column("branches", sa.Column("closes_at", sa.Time(), nullable=True))
    op.add_column(
        "branches",
        sa.Column(
            "working_days",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
            server_default="[0, 1, 2, 3, 4, 5, 6]",
        ),
    )
    op.alter_column("branches", "working_days", server_default=None)


def downgrade() -> None:
    op.drop_column("branches", "working_days")
    op.drop_column("branches", "closes_at")
    op.drop_column("branches", "opens_at")
