"""add group ordering and added_by_user_id tracking

Revision ID: 7ccb075c7099
Revises: 33c74347c7c1
Create Date: 2026-08-03 11:40:32.602427

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '7ccb075c7099'
down_revision: Union[str, None] = '33c74347c7c1'
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('group_order_sessions',
    sa.Column('id', sa.Uuid(), nullable=False),
    sa.Column('cart_id', sa.Uuid(), nullable=False),
    sa.Column('host_id', sa.Uuid(), nullable=False),
    sa.Column('join_code', sa.String(length=8), nullable=False),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('deadline', sa.DateTime(), nullable=True),
    sa.Column('per_person_limit', sa.Numeric(precision=10, scale=2), 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(['cart_id'], ['carts.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['host_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_group_order_sessions_host_id'), 'group_order_sessions', ['host_id'], unique=False)
    op.create_index(op.f('ix_group_order_sessions_join_code'), 'group_order_sessions', ['join_code'], unique=True)
    op.create_table('group_order_participants',
    sa.Column('id', sa.Uuid(), nullable=False),
    sa.Column('session_id', sa.Uuid(), nullable=False),
    sa.Column('user_id', sa.Uuid(), nullable=False),
    sa.Column('joined_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['session_id'], ['group_order_sessions.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('session_id', 'user_id')
    )
    op.create_index(op.f('ix_group_order_participants_session_id'), 'group_order_participants', ['session_id'], unique=False)
    op.create_index(op.f('ix_group_order_participants_user_id'), 'group_order_participants', ['user_id'], unique=False)

    # added_by_user_id added nullable first, backfilled from the
    # owning cart/order's own customer, then locked to NOT NULL — a
    # straight NOT NULL add would fail outright against any existing
    # rows (no default), and even where it wouldn't, every pre-existing
    # row here really was added by that cart/order's own customer
    # (group ordering didn't exist yet), so this is the correct value,
    # not a placeholder.
    op.add_column('cart_items', sa.Column('added_by_user_id', sa.Uuid(), nullable=True))
    op.execute(
        "UPDATE cart_items SET added_by_user_id = carts.customer_id "
        "FROM carts WHERE carts.id = cart_items.cart_id"
    )
    op.alter_column('cart_items', 'added_by_user_id', nullable=False)
    op.create_foreign_key(None, 'cart_items', 'users', ['added_by_user_id'], ['id'], ondelete='CASCADE')

    op.add_column('order_items', sa.Column('added_by_user_id', sa.Uuid(), nullable=True))
    op.execute(
        "UPDATE order_items SET added_by_user_id = orders.customer_id "
        "FROM orders WHERE orders.id = order_items.order_id"
    )
    op.alter_column('order_items', 'added_by_user_id', nullable=False)
    op.create_foreign_key(None, 'order_items', 'users', ['added_by_user_id'], ['id'], ondelete='CASCADE')

    op.add_column('orders', sa.Column('group_order_session_id', sa.Uuid(), nullable=True))
    op.create_index(op.f('ix_orders_group_order_session_id'), 'orders', ['group_order_session_id'], unique=False)
    op.create_foreign_key(None, 'orders', 'group_order_sessions', ['group_order_session_id'], ['id'], ondelete='SET NULL')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'orders', type_='foreignkey')
    op.drop_index(op.f('ix_orders_group_order_session_id'), table_name='orders')
    op.drop_column('orders', 'group_order_session_id')
    op.drop_constraint(None, 'order_items', type_='foreignkey')
    op.drop_column('order_items', 'added_by_user_id')
    op.drop_constraint(None, 'cart_items', type_='foreignkey')
    op.drop_column('cart_items', 'added_by_user_id')
    op.drop_index(op.f('ix_group_order_participants_user_id'), table_name='group_order_participants')
    op.drop_index(op.f('ix_group_order_participants_session_id'), table_name='group_order_participants')
    op.drop_table('group_order_participants')
    op.drop_index(op.f('ix_group_order_sessions_join_code'), table_name='group_order_sessions')
    op.drop_index(op.f('ix_group_order_sessions_host_id'), table_name='group_order_sessions')
    op.drop_table('group_order_sessions')
    # ### end Alembic commands ###
