"""add courier profiles and delivery assignments

Revision ID: 18830e0b291f
Revises: 8625b9cb6638
Create Date: 2026-07-16 17:02:30.067951

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "18830e0b291f"
down_revision: Union[str, None] = "8625b9cb6638"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "courier_profiles",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("user_id", sa.Uuid(), nullable=False),
        sa.Column("shift_start", sa.Time(), nullable=True),
        sa.Column("shift_end", sa.Time(), nullable=True),
        sa.Column("last_latitude", sa.Float(), nullable=True),
        sa.Column("last_longitude", sa.Float(), nullable=True),
        sa.Column("last_location_at", sa.DateTime(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column(
            "updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id"),
    )
    op.create_table(
        "delivery_assignments",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("order_id", sa.Uuid(), nullable=False),
        sa.Column("courier_id", sa.Uuid(), nullable=False),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column(
            "offered_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("responded_at", sa.DateTime(), nullable=True),
        sa.Column("picked_up_at", sa.DateTime(), nullable=True),
        sa.Column("delivered_at", sa.DateTime(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.ForeignKeyConstraint(
            ["courier_id"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(["order_id"], ["orders.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_delivery_assignments_courier_id"),
        "delivery_assignments",
        ["courier_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_delivery_assignments_order_id"),
        "delivery_assignments",
        ["order_id"],
        unique=False,
    )
    # 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/users_phone_key (pre-existing
    # unique-index-vs-named-constraint drift unrelated to this migration).
    # Both removed by hand — same drift called out in 42828b3f2eea.
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_delivery_assignments_order_id"), table_name="delivery_assignments"
    )
    op.drop_index(
        op.f("ix_delivery_assignments_courier_id"), table_name="delivery_assignments"
    )
    op.drop_table("delivery_assignments")
    op.drop_table("courier_profiles")
    # ### end Alembic commands ###
