"""Initial migration with paddle_ocr prefix

Revision ID: a7802b126240
Revises: 
Create Date: 2025-11-12 00:46:58.519941

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision: str = 'a7802b126240'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('paddle_ocr_users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(length=50), nullable=False),
    sa.Column('email', sa.String(length=100), nullable=False),
    sa.Column('password_hash', sa.String(length=255), nullable=False),
    sa.Column('full_name', sa.String(length=100), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_admin', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_paddle_ocr_users_email'), 'paddle_ocr_users', ['email'], unique=True)
    op.create_index(op.f('ix_paddle_ocr_users_id'), 'paddle_ocr_users', ['id'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_users_username'), 'paddle_ocr_users', ['username'], unique=True)
    op.create_table('paddle_ocr_batches',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('batch_name', sa.String(length=255), nullable=True),
    sa.Column('status', sa.Enum('PENDING', 'PROCESSING', 'COMPLETED', 'PARTIAL', 'FAILED', name='batchstatus'), nullable=False),
    sa.Column('total_files', sa.Integer(), nullable=False),
    sa.Column('completed_files', sa.Integer(), nullable=False),
    sa.Column('failed_files', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('started_at', sa.DateTime(), nullable=True),
    sa.Column('completed_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['paddle_ocr_users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_paddle_ocr_batches_created_at'), 'paddle_ocr_batches', ['created_at'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_batches_id'), 'paddle_ocr_batches', ['id'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_batches_status'), 'paddle_ocr_batches', ['status'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_batches_user_id'), 'paddle_ocr_batches', ['user_id'], unique=False)
    op.create_table('paddle_ocr_export_rules',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('rule_name', sa.String(length=100), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('config_json', sa.JSON(), nullable=False),
    sa.Column('css_template', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['paddle_ocr_users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_paddle_ocr_export_rules_id'), 'paddle_ocr_export_rules', ['id'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_export_rules_user_id'), 'paddle_ocr_export_rules', ['user_id'], unique=False)
    op.create_table('paddle_ocr_translation_configs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('source_lang', sa.String(length=20), nullable=False),
    sa.Column('target_lang', sa.String(length=20), nullable=False),
    sa.Column('engine_type', sa.String(length=50), nullable=False),
    sa.Column('engine_config', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['paddle_ocr_users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_paddle_ocr_translation_configs_id'), 'paddle_ocr_translation_configs', ['id'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_translation_configs_user_id'), 'paddle_ocr_translation_configs', ['user_id'], unique=False)
    op.create_table('paddle_ocr_files',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('batch_id', sa.Integer(), nullable=False),
    sa.Column('filename', sa.String(length=255), nullable=False),
    sa.Column('original_filename', sa.String(length=255), nullable=False),
    sa.Column('file_path', sa.String(length=512), nullable=False),
    sa.Column('file_size', sa.Integer(), nullable=False),
    sa.Column('file_format', sa.String(length=20), nullable=False),
    sa.Column('status', sa.Enum('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED', name='filestatus'), nullable=False),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('started_at', sa.DateTime(), nullable=True),
    sa.Column('completed_at', sa.DateTime(), nullable=True),
    sa.Column('processing_time', sa.Float(), nullable=True),
    sa.ForeignKeyConstraint(['batch_id'], ['paddle_ocr_batches.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_paddle_ocr_files_batch_id'), 'paddle_ocr_files', ['batch_id'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_files_id'), 'paddle_ocr_files', ['id'], unique=False)
    op.create_index(op.f('ix_paddle_ocr_files_status'), 'paddle_ocr_files', ['status'], unique=False)
    op.create_table('paddle_ocr_results',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('file_id', sa.Integer(), nullable=False),
    sa.Column('markdown_path', sa.String(length=512), nullable=True),
    sa.Column('json_path', sa.String(length=512), nullable=True),
    sa.Column('images_dir', sa.String(length=512), nullable=True),
    sa.Column('detected_language', sa.String(length=20), nullable=True),
    sa.Column('total_text_regions', sa.Integer(), nullable=False),
    sa.Column('average_confidence', sa.Float(), nullable=True),
    sa.Column('layout_data', sa.JSON(), nullable=True),
    sa.Column('images_metadata', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['file_id'], ['paddle_ocr_files.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_paddle_ocr_results_file_id'), 'paddle_ocr_results', ['file_id'], unique=True)
    op.create_index(op.f('ix_paddle_ocr_results_id'), 'paddle_ocr_results', ['id'], unique=False)
    # NOTE: Removed all drop_table/drop_index commands to preserve existing tables in shared database
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema - removes all paddle_ocr_ tables."""
    # ### commands auto generated by Alembic - please adjust! ###
    # Drop paddle_ocr tables in reverse order
    op.drop_index(op.f('ix_paddle_ocr_results_id'), table_name='paddle_ocr_results')
    op.drop_index(op.f('ix_paddle_ocr_results_file_id'), table_name='paddle_ocr_results')
    op.drop_table('paddle_ocr_results')
    op.drop_index(op.f('ix_paddle_ocr_files_status'), table_name='paddle_ocr_files')
    op.drop_index(op.f('ix_paddle_ocr_files_id'), table_name='paddle_ocr_files')
    op.drop_index(op.f('ix_paddle_ocr_files_batch_id'), table_name='paddle_ocr_files')
    op.drop_table('paddle_ocr_files')
    op.drop_index(op.f('ix_paddle_ocr_translation_configs_user_id'), table_name='paddle_ocr_translation_configs')
    op.drop_index(op.f('ix_paddle_ocr_translation_configs_id'), table_name='paddle_ocr_translation_configs')
    op.drop_table('paddle_ocr_translation_configs')
    op.drop_index(op.f('ix_paddle_ocr_export_rules_user_id'), table_name='paddle_ocr_export_rules')
    op.drop_index(op.f('ix_paddle_ocr_export_rules_id'), table_name='paddle_ocr_export_rules')
    op.drop_table('paddle_ocr_export_rules')
    op.drop_index(op.f('ix_paddle_ocr_batches_user_id'), table_name='paddle_ocr_batches')
    op.drop_index(op.f('ix_paddle_ocr_batches_status'), table_name='paddle_ocr_batches')
    op.drop_index(op.f('ix_paddle_ocr_batches_id'), table_name='paddle_ocr_batches')
    op.drop_index(op.f('ix_paddle_ocr_batches_created_at'), table_name='paddle_ocr_batches')
    op.drop_table('paddle_ocr_batches')
    op.drop_index(op.f('ix_paddle_ocr_users_username'), table_name='paddle_ocr_users')
    op.drop_index(op.f('ix_paddle_ocr_users_id'), table_name='paddle_ocr_users')
    op.drop_index(op.f('ix_paddle_ocr_users_email'), table_name='paddle_ocr_users')
    op.drop_table('paddle_ocr_users')
    # NOTE: We do NOT recreate other tables that existed before this migration
    # ### end Alembic commands ###

