"""Add batch processing fields to receipts table.

Revision ID: add_batch_processing_fields
Revises: add_original_filename_to_metrics
Create Date: 2026-01-11

Adds fields for bulk upload batch tracking:
- batch_id: UUID string for grouping receipts from same upload
- processing_status: enum (pending/processing/completed/failed)
- processing_error: full error message text
- file_hash: SHA-256 hash for duplicate detection
- processing_started_at: when OCR processing started
- processing_completed_at: when OCR processing completed

Also creates indexes for efficient querying.
"""
from alembic import op
import sqlalchemy as sa


# Revision identifiers
revision = 'add_batch_processing_fields'
down_revision = 'add_original_filename_to_metrics'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Add batch processing columns to receipts table."""
    conn = op.get_bind()
    inspector = sa.inspect(conn)

    # Get existing columns
    columns = [col['name'] for col in inspector.get_columns('receipts')]

    # Add batch_id column with index
    if 'batch_id' not in columns:
        op.add_column(
            'receipts',
            sa.Column('batch_id', sa.String(length=50), nullable=True)
        )
        op.create_index(
            'ix_receipts_batch_id',
            'receipts',
            ['batch_id'],
            unique=False
        )

    # Add processing_status column with index
    if 'processing_status' not in columns:
        op.add_column(
            'receipts',
            sa.Column('processing_status', sa.String(length=20), nullable=True)
        )
        op.create_index(
            'ix_receipts_processing_status',
            'receipts',
            ['processing_status'],
            unique=False
        )

    # Add processing_error column (TEXT for full error messages)
    if 'processing_error' not in columns:
        op.add_column(
            'receipts',
            sa.Column('processing_error', sa.Text(), nullable=True)
        )

    # Add file_hash column with index for duplicate detection
    if 'file_hash' not in columns:
        op.add_column(
            'receipts',
            sa.Column('file_hash', sa.String(length=64), nullable=True)
        )
        op.create_index(
            'ix_receipts_file_hash',
            'receipts',
            ['file_hash'],
            unique=False
        )

    # Add processing_started_at column
    if 'processing_started_at' not in columns:
        op.add_column(
            'receipts',
            sa.Column('processing_started_at', sa.DateTime(), nullable=True)
        )

    # Add processing_completed_at column
    if 'processing_completed_at' not in columns:
        op.add_column(
            'receipts',
            sa.Column('processing_completed_at', sa.DateTime(), nullable=True)
        )


def downgrade() -> None:
    """Remove batch processing columns from receipts table."""
    conn = op.get_bind()
    inspector = sa.inspect(conn)

    columns = [col['name'] for col in inspector.get_columns('receipts')]
    indexes = [idx['name'] for idx in inspector.get_indexes('receipts')]

    # Remove indexes first (SQLite batch mode)
    if 'ix_receipts_batch_id' in indexes:
        op.drop_index('ix_receipts_batch_id', table_name='receipts')
    if 'ix_receipts_processing_status' in indexes:
        op.drop_index('ix_receipts_processing_status', table_name='receipts')
    if 'ix_receipts_file_hash' in indexes:
        op.drop_index('ix_receipts_file_hash', table_name='receipts')

    # Remove columns (in reverse order of addition)
    if 'processing_completed_at' in columns:
        op.drop_column('receipts', 'processing_completed_at')
    if 'processing_started_at' in columns:
        op.drop_column('receipts', 'processing_started_at')
    if 'file_hash' in columns:
        op.drop_column('receipts', 'file_hash')
    if 'processing_error' in columns:
        op.drop_column('receipts', 'processing_error')
    if 'processing_status' in columns:
        op.drop_column('receipts', 'processing_status')
    if 'batch_id' in columns:
        op.drop_column('receipts', 'batch_id')
