"""unified initial production schema

Revision ID: 001_unified_initial_schema
Revises:
Create Date: 2025-11-07

Complete production service schema including:
- Production batches (with reasoning_data for i18n JTBD dashboard and waste tracking)
- Production schedules
- Production capacity
- Equipment
- Quality checks and templates
- Audit logs
"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = '001_unified_initial_schema'
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:
    # Create audit_logs table
    op.create_table('audit_logs',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('user_id', sa.UUID(), nullable=False),
        sa.Column('action', sa.String(length=100), nullable=False),
        sa.Column('resource_type', sa.String(length=100), nullable=False),
        sa.Column('resource_id', sa.String(length=255), nullable=True),
        sa.Column('severity', sa.String(length=20), nullable=False),
        sa.Column('service_name', sa.String(length=100), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('changes', postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column('audit_metadata', postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column('ip_address', sa.String(length=45), nullable=True),
        sa.Column('user_agent', sa.Text(), nullable=True),
        sa.Column('endpoint', sa.String(length=255), nullable=True),
        sa.Column('method', sa.String(length=10), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_audit_resource_type_action', 'audit_logs', ['resource_type', 'action'], unique=False)
    op.create_index('idx_audit_service_created', 'audit_logs', ['service_name', 'created_at'], unique=False)
    op.create_index('idx_audit_severity_created', 'audit_logs', ['severity', 'created_at'], unique=False)
    op.create_index('idx_audit_tenant_created', 'audit_logs', ['tenant_id', 'created_at'], unique=False)
    op.create_index('idx_audit_user_created', 'audit_logs', ['user_id', 'created_at'], unique=False)
    op.create_index(op.f('ix_audit_logs_action'), 'audit_logs', ['action'], unique=False)
    op.create_index(op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'], unique=False)
    op.create_index(op.f('ix_audit_logs_resource_id'), 'audit_logs', ['resource_id'], unique=False)
    op.create_index(op.f('ix_audit_logs_resource_type'), 'audit_logs', ['resource_type'], unique=False)
    op.create_index(op.f('ix_audit_logs_service_name'), 'audit_logs', ['service_name'], unique=False)
    op.create_index(op.f('ix_audit_logs_severity'), 'audit_logs', ['severity'], unique=False)
    op.create_index(op.f('ix_audit_logs_tenant_id'), 'audit_logs', ['tenant_id'], unique=False)
    op.create_index(op.f('ix_audit_logs_user_id'), 'audit_logs', ['user_id'], unique=False)

    # Create equipment table
    op.create_table('equipment',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=255), nullable=False),
        sa.Column('type', sa.Enum('OVEN', 'MIXER', 'PROOFER', 'FREEZER', 'PACKAGING', 'OTHER', name='equipmenttype'), nullable=False),
        sa.Column('model', sa.String(length=100), nullable=True),
        sa.Column('serial_number', sa.String(length=100), nullable=True),
        sa.Column('location', sa.String(length=255), nullable=True),
        sa.Column('status', sa.Enum('OPERATIONAL', 'MAINTENANCE', 'DOWN', 'WARNING', name='equipmentstatus'), nullable=False),
        sa.Column('install_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('last_maintenance_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('next_maintenance_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('maintenance_interval_days', sa.Integer(), nullable=True),
        sa.Column('efficiency_percentage', sa.Float(), nullable=True),
        sa.Column('uptime_percentage', sa.Float(), nullable=True),
        sa.Column('energy_usage_kwh', sa.Float(), nullable=True),
        sa.Column('power_kw', sa.Float(), nullable=True),
        sa.Column('capacity', sa.Float(), nullable=True),
        sa.Column('weight_kg', sa.Float(), nullable=True),
        sa.Column('current_temperature', sa.Float(), nullable=True),
        sa.Column('target_temperature', sa.Float(), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('support_contact', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_equipment_tenant_id'), 'equipment', ['tenant_id'], unique=False)

    # Create production_batches table (with reasoning_data for i18n and waste tracking)
    op.create_table('production_batches',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('batch_number', sa.String(length=50), nullable=False),
        sa.Column('product_id', sa.UUID(), nullable=False),
        sa.Column('product_name', sa.String(length=255), nullable=False),
        sa.Column('recipe_id', sa.UUID(), nullable=True),
        sa.Column('planned_start_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('planned_end_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('planned_quantity', sa.Float(), nullable=False),
        sa.Column('planned_duration_minutes', sa.Integer(), nullable=False),
        sa.Column('actual_start_time', sa.DateTime(timezone=True), nullable=True),
        sa.Column('actual_end_time', sa.DateTime(timezone=True), nullable=True),
        sa.Column('actual_quantity', sa.Float(), nullable=True),
        sa.Column('actual_duration_minutes', sa.Integer(), nullable=True),
        sa.Column('status', sa.Enum('PENDING', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'ON_HOLD', 'QUALITY_CHECK', 'FAILED', name='productionstatus'), nullable=False),
        sa.Column('priority', sa.Enum('LOW', 'MEDIUM', 'HIGH', 'URGENT', name='productionpriority'), nullable=False),
        sa.Column('current_process_stage', sa.Enum('MIXING', 'PROOFING', 'SHAPING', 'BAKING', 'COOLING', 'PACKAGING', 'FINISHING', name='processstage'), nullable=True),
        sa.Column('process_stage_history', sa.JSON(), nullable=True),
        sa.Column('pending_quality_checks', sa.JSON(), nullable=True),
        sa.Column('completed_quality_checks', sa.JSON(), nullable=True),
        sa.Column('estimated_cost', sa.Float(), nullable=True),
        sa.Column('actual_cost', sa.Float(), nullable=True),
        sa.Column('labor_cost', sa.Float(), nullable=True),
        sa.Column('material_cost', sa.Float(), nullable=True),
        sa.Column('overhead_cost', sa.Float(), nullable=True),
        sa.Column('yield_percentage', sa.Float(), nullable=True),
        sa.Column('quality_score', sa.Float(), nullable=True),
        sa.Column('waste_quantity', sa.Float(), nullable=True),
        sa.Column('defect_quantity', sa.Float(), nullable=True),
        # Waste tracking fields (from 20251023_0900 migration)
        sa.Column('waste_defect_type', sa.String(length=100), nullable=True),
        sa.Column('is_ai_assisted', sa.Boolean(), nullable=False, server_default='false'),
        sa.Column('equipment_used', sa.JSON(), nullable=True),
        sa.Column('staff_assigned', sa.JSON(), nullable=True),
        sa.Column('station_id', sa.String(length=50), nullable=True),
        sa.Column('order_id', sa.UUID(), nullable=True),
        sa.Column('forecast_id', sa.UUID(), nullable=True),
        sa.Column('is_rush_order', sa.Boolean(), nullable=True),
        sa.Column('is_special_recipe', sa.Boolean(), nullable=True),
        sa.Column('production_notes', sa.Text(), nullable=True),
        sa.Column('quality_notes', sa.Text(), nullable=True),
        sa.Column('delay_reason', sa.String(length=255), nullable=True),
        sa.Column('cancellation_reason', sa.String(length=255), nullable=True),
        # JTBD Dashboard: Structured reasoning for i18n support
        sa.Column('reasoning_data', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_production_batches_batch_number'), 'production_batches', ['batch_number'], unique=True)
    op.create_index(op.f('ix_production_batches_current_process_stage'), 'production_batches', ['current_process_stage'], unique=False)
    op.create_index(op.f('ix_production_batches_product_id'), 'production_batches', ['product_id'], unique=False)
    op.create_index(op.f('ix_production_batches_status'), 'production_batches', ['status'], unique=False)
    op.create_index(op.f('ix_production_batches_tenant_id'), 'production_batches', ['tenant_id'], unique=False)
    op.create_index('ix_production_batches_is_ai_assisted', 'production_batches', ['is_ai_assisted'], unique=False)

    # Create production_capacity table
    op.create_table('production_capacity',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('resource_type', sa.String(length=50), nullable=False),
        sa.Column('resource_id', sa.String(length=100), nullable=False),
        sa.Column('resource_name', sa.String(length=255), nullable=False),
        sa.Column('date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('start_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('end_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('total_capacity_units', sa.Float(), nullable=False),
        sa.Column('allocated_capacity_units', sa.Float(), nullable=False),
        sa.Column('remaining_capacity_units', sa.Float(), nullable=False),
        sa.Column('is_available', sa.Boolean(), nullable=True),
        sa.Column('is_maintenance', sa.Boolean(), nullable=True),
        sa.Column('is_reserved', sa.Boolean(), nullable=True),
        sa.Column('equipment_type', sa.String(length=100), nullable=True),
        sa.Column('max_batch_size', sa.Float(), nullable=True),
        sa.Column('min_batch_size', sa.Float(), nullable=True),
        sa.Column('setup_time_minutes', sa.Integer(), nullable=True),
        sa.Column('cleanup_time_minutes', sa.Integer(), nullable=True),
        sa.Column('efficiency_rating', sa.Float(), nullable=True),
        sa.Column('maintenance_status', sa.String(length=50), nullable=True),
        sa.Column('last_maintenance_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('notes', sa.Text(), nullable=True),
        sa.Column('restrictions', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_production_capacity_date'), 'production_capacity', ['date'], unique=False)
    op.create_index(op.f('ix_production_capacity_tenant_id'), 'production_capacity', ['tenant_id'], unique=False)

    # Create production_schedules table
    op.create_table('production_schedules',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('schedule_date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('shift_start', sa.DateTime(timezone=True), nullable=False),
        sa.Column('shift_end', sa.DateTime(timezone=True), nullable=False),
        sa.Column('total_capacity_hours', sa.Float(), nullable=False),
        sa.Column('planned_capacity_hours', sa.Float(), nullable=False),
        sa.Column('actual_capacity_hours', sa.Float(), nullable=True),
        sa.Column('overtime_hours', sa.Float(), nullable=True),
        sa.Column('staff_count', sa.Integer(), nullable=False),
        sa.Column('equipment_capacity', sa.JSON(), nullable=True),
        sa.Column('station_assignments', sa.JSON(), nullable=True),
        sa.Column('total_batches_planned', sa.Integer(), nullable=False),
        sa.Column('total_batches_completed', sa.Integer(), nullable=True),
        sa.Column('total_quantity_planned', sa.Float(), nullable=False),
        sa.Column('total_quantity_produced', sa.Float(), nullable=True),
        sa.Column('is_finalized', sa.Boolean(), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=True),
        sa.Column('efficiency_percentage', sa.Float(), nullable=True),
        sa.Column('utilization_percentage', sa.Float(), nullable=True),
        sa.Column('on_time_completion_rate', sa.Float(), nullable=True),
        sa.Column('schedule_notes', sa.Text(), nullable=True),
        sa.Column('schedule_adjustments', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('finalized_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_production_schedules_schedule_date'), 'production_schedules', ['schedule_date'], unique=False)
    op.create_index(op.f('ix_production_schedules_tenant_id'), 'production_schedules', ['tenant_id'], unique=False)

    # Create quality_check_templates table
    op.create_table('quality_check_templates',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('name', sa.String(length=255), nullable=False),
        sa.Column('template_code', sa.String(length=100), nullable=True),
        sa.Column('check_type', sa.String(length=50), nullable=False),
        sa.Column('category', sa.String(length=100), nullable=True),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('instructions', sa.Text(), nullable=True),
        sa.Column('parameters', sa.JSON(), nullable=True),
        sa.Column('thresholds', sa.JSON(), nullable=True),
        sa.Column('scoring_criteria', sa.JSON(), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=True),
        sa.Column('is_required', sa.Boolean(), nullable=True),
        sa.Column('is_critical', sa.Boolean(), nullable=True),
        sa.Column('weight', sa.Float(), nullable=True),
        sa.Column('min_value', sa.Float(), nullable=True),
        sa.Column('max_value', sa.Float(), nullable=True),
        sa.Column('target_value', sa.Float(), nullable=True),
        sa.Column('unit', sa.String(length=20), nullable=True),
        sa.Column('tolerance_percentage', sa.Float(), nullable=True),
        sa.Column('applicable_stages', sa.JSON(), nullable=True),
        sa.Column('created_by', sa.UUID(), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_quality_check_templates_template_code'), 'quality_check_templates', ['template_code'], unique=False)
    op.create_index(op.f('ix_quality_check_templates_tenant_id'), 'quality_check_templates', ['tenant_id'], unique=False)

    # Create quality_checks table
    op.create_table('quality_checks',
        sa.Column('id', sa.UUID(), nullable=False),
        sa.Column('tenant_id', sa.UUID(), nullable=False),
        sa.Column('batch_id', sa.UUID(), nullable=False),
        sa.Column('template_id', sa.UUID(), nullable=True),
        sa.Column('check_type', sa.String(length=50), nullable=False),
        sa.Column('process_stage', sa.Enum('MIXING', 'PROOFING', 'SHAPING', 'BAKING', 'COOLING', 'PACKAGING', 'FINISHING', name='processstage'), nullable=True),
        sa.Column('check_time', sa.DateTime(timezone=True), nullable=False),
        sa.Column('checker_id', sa.String(length=100), nullable=True),
        sa.Column('quality_score', sa.Float(), nullable=False),
        sa.Column('pass_fail', sa.Boolean(), nullable=False),
        sa.Column('defect_count', sa.Integer(), nullable=False),
        sa.Column('defect_types', sa.JSON(), nullable=True),
        sa.Column('measured_weight', sa.Float(), nullable=True),
        sa.Column('measured_temperature', sa.Float(), nullable=True),
        sa.Column('measured_moisture', sa.Float(), nullable=True),
        sa.Column('measured_dimensions', sa.JSON(), nullable=True),
        sa.Column('stage_specific_data', sa.JSON(), nullable=True),
        sa.Column('target_weight', sa.Float(), nullable=True),
        sa.Column('target_temperature', sa.Float(), nullable=True),
        sa.Column('target_moisture', sa.Float(), nullable=True),
        sa.Column('tolerance_percentage', sa.Float(), nullable=True),
        sa.Column('within_tolerance', sa.Boolean(), nullable=True),
        sa.Column('corrective_action_needed', sa.Boolean(), nullable=True),
        sa.Column('corrective_actions', sa.JSON(), nullable=True),
        sa.Column('template_results', sa.JSON(), nullable=True),
        sa.Column('criteria_scores', sa.JSON(), nullable=True),
        sa.Column('check_notes', sa.Text(), nullable=True),
        sa.Column('photos_urls', sa.JSON(), nullable=True),
        sa.Column('certificate_url', sa.String(length=500), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_quality_checks_batch_id'), 'quality_checks', ['batch_id'], unique=False)
    op.create_index(op.f('ix_quality_checks_process_stage'), 'quality_checks', ['process_stage'], unique=False)
    op.create_index(op.f('ix_quality_checks_template_id'), 'quality_checks', ['template_id'], unique=False)
    op.create_index(op.f('ix_quality_checks_tenant_id'), 'quality_checks', ['tenant_id'], unique=False)


def downgrade() -> None:
    # Drop tables in reverse order of creation
    op.drop_index(op.f('ix_quality_checks_tenant_id'), table_name='quality_checks')
    op.drop_index(op.f('ix_quality_checks_template_id'), table_name='quality_checks')
    op.drop_index(op.f('ix_quality_checks_process_stage'), table_name='quality_checks')
    op.drop_index(op.f('ix_quality_checks_batch_id'), table_name='quality_checks')
    op.drop_table('quality_checks')
    op.drop_index(op.f('ix_quality_check_templates_tenant_id'), table_name='quality_check_templates')
    op.drop_index(op.f('ix_quality_check_templates_template_code'), table_name='quality_check_templates')
    op.drop_table('quality_check_templates')
    op.drop_index(op.f('ix_production_schedules_tenant_id'), table_name='production_schedules')
    op.drop_index(op.f('ix_production_schedules_schedule_date'), table_name='production_schedules')
    op.drop_table('production_schedules')
    op.drop_index(op.f('ix_production_capacity_tenant_id'), table_name='production_capacity')
    op.drop_index(op.f('ix_production_capacity_date'), table_name='production_capacity')
    op.drop_table('production_capacity')
    op.drop_index('ix_production_batches_is_ai_assisted', table_name='production_batches')
    op.drop_index(op.f('ix_production_batches_tenant_id'), table_name='production_batches')
    op.drop_index(op.f('ix_production_batches_status'), table_name='production_batches')
    op.drop_index(op.f('ix_production_batches_product_id'), table_name='production_batches')
    op.drop_index(op.f('ix_production_batches_current_process_stage'), table_name='production_batches')
    op.drop_index(op.f('ix_production_batches_batch_number'), table_name='production_batches')
    op.drop_table('production_batches')
    op.drop_index(op.f('ix_equipment_tenant_id'), table_name='equipment')
    op.drop_table('equipment')
    op.drop_index(op.f('ix_audit_logs_user_id'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_tenant_id'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_severity'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_service_name'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_resource_type'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_resource_id'), table_name='audit_logs')
    op.create_index(op.f('ix_audit_logs_created_at'), table_name='audit_logs')
    op.drop_index(op.f('ix_audit_logs_action'), table_name='audit_logs')
    op.drop_index('idx_audit_user_created', table_name='audit_logs')
    op.drop_index('idx_audit_tenant_created', table_name='audit_logs')
    op.drop_index('idx_audit_severity_created', table_name='audit_logs')
    op.drop_index('idx_audit_service_created', table_name='audit_logs')
    op.drop_index('idx_audit_resource_type_action', table_name='audit_logs')
    op.drop_table('audit_logs')

    # Drop enum types
    op.execute("DROP TYPE IF EXISTS equipmenttype")
    op.execute("DROP TYPE IF EXISTS equipmentstatus")
    op.execute("DROP TYPE IF EXISTS productionstatus")
    op.execute("DROP TYPE IF EXISTS productionpriority")
    op.execute("DROP TYPE IF EXISTS processstage")
