"""add tickets table

Revision ID: f6d4a1b2c3e7
Revises: e5c3f9a2b1d6
Create Date: 2026-02-09
"""
from alembic import op
import sqlalchemy as sa

revision = 'f6d4a1b2c3e7'
down_revision = 'e5c3f9a2b1d6'
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        'tickets',
        sa.Column('id', sa.Integer(), primary_key=True),
        sa.Column('entry_id', sa.Integer(), sa.ForeignKey('calendar_entries.id', ondelete='CASCADE'), nullable=False),
        sa.Column('ticket_type_id', sa.Integer(), sa.ForeignKey('ticket_types.id', ondelete='SET NULL'), nullable=True),
        sa.Column('user_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True),
        sa.Column('session_id', sa.String(64), nullable=True),
        sa.Column('order_id', sa.Integer(), sa.ForeignKey('orders.id', ondelete='SET NULL'), nullable=True),
        sa.Column('code', sa.String(64), unique=True, nullable=False),
        sa.Column('state', sa.String(20), nullable=False, server_default=sa.text("'reserved'")),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column('checked_in_at', sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index('ix_tickets_entry_id', 'tickets', ['entry_id'])
    op.create_index('ix_tickets_ticket_type_id', 'tickets', ['ticket_type_id'])
    op.create_index('ix_tickets_user_id', 'tickets', ['user_id'])
    op.create_index('ix_tickets_session_id', 'tickets', ['session_id'])
    op.create_index('ix_tickets_order_id', 'tickets', ['order_id'])
    op.create_index('ix_tickets_code', 'tickets', ['code'], unique=True)
    op.create_index('ix_tickets_state', 'tickets', ['state'])


def downgrade() -> None:
    op.drop_index('ix_tickets_state', 'tickets')
    op.drop_index('ix_tickets_code', 'tickets')
    op.drop_index('ix_tickets_order_id', 'tickets')
    op.drop_index('ix_tickets_session_id', 'tickets')
    op.drop_index('ix_tickets_user_id', 'tickets')
    op.drop_index('ix_tickets_ticket_type_id', 'tickets')
    op.drop_index('ix_tickets_entry_id', 'tickets')
    op.drop_table('tickets')
