"""Add chat conversations and messages tables

Revision ID: e752a41dc38e
Revises: 65b646c357d7
Create Date: 2026-06-30 01:14:29.228496

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'e752a41dc38e'
down_revision = '65b646c357d7'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('conversations',
    sa.Column('listing_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('buyer_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('seller_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('status', sa.Enum('active', 'closed', name='conversation_status'), nullable=False),
    sa.Column('conversation_started_at', sa.DateTime(), nullable=False),
    sa.Column('first_reply_at', sa.DateTime(), nullable=True),
    sa.Column('free_messages_remaining', sa.Integer(), nullable=False),
    sa.Column('rewarded_messages_remaining', sa.Integer(), nullable=False),
    sa.Column('ads_watched', sa.Integer(), nullable=False),
    sa.Column('is_premium_conversation', sa.Boolean(), nullable=False),
    sa.Column('last_message_at', sa.DateTime(), nullable=True),
    sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False),
    sa.Column('public_id', sa.CHAR(length=36), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['buyer_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['listing_id'], ['listings.id'], ),
    sa.ForeignKeyConstraint(['seller_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('buyer_id', 'seller_id', 'listing_id', name='uq_conversations_buyer_seller_listing'),
    sa.UniqueConstraint('public_id')
    )
    with op.batch_alter_table('conversations', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_conversations_buyer_id'), ['buyer_id'], unique=False)
        batch_op.create_index('ix_conversations_created_at', ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_conversations_is_premium_conversation'), ['is_premium_conversation'], unique=False)
        batch_op.create_index(batch_op.f('ix_conversations_last_message_at'), ['last_message_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_conversations_listing_id'), ['listing_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_conversations_seller_id'), ['seller_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_conversations_status'), ['status'], unique=False)

    op.create_table('messages',
    sa.Column('conversation_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('sender_id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
    sa.Column('message', sa.Text(), nullable=False),
    sa.Column('message_type', sa.Enum('text', 'system', name='message_type'), nullable=False),
    sa.Column('read_at', sa.DateTime(), nullable=True),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), autoincrement=True, nullable=False),
    sa.Column('public_id', sa.CHAR(length=36), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
    sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('public_id')
    )
    with op.batch_alter_table('messages', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_messages_conversation_id'), ['conversation_id'], unique=False)
        batch_op.create_index('ix_messages_created_at', ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_messages_deleted_at'), ['deleted_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_messages_message_type'), ['message_type'], unique=False)
        batch_op.create_index(batch_op.f('ix_messages_read_at'), ['read_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_messages_sender_id'), ['sender_id'], unique=False)

    # ### end Alembic commands ###


def downgrade():
    # Drop tables only (each drop removes its own indexes/FKs). Messages first
    # because it references conversations. Dropping FK-backing indexes
    # explicitly fails on MySQL (errno 1553), so we avoid it.
    op.drop_table('messages')
    op.drop_table('conversations')
