# ================================================================
# services/forecasting/migrations/versions/001_initial_tables.py
# ================================================================
"""Initial forecasting tables

Revision ID: 001
Revises: 
Create Date: 2024-01-15 10:00:00.000000

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

# revision identifiers
revision = '001'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # Create forecasts table
    op.create_table('forecasts',
        sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('product_name', sa.String(length=255), nullable=False),
        sa.Column('location', sa.String(length=255), nullable=False),
        sa.Column('forecast_date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('predicted_demand', sa.Float(), nullable=False),
        sa.Column('confidence_lower', sa.Float(), nullable=False),
        sa.Column('confidence_upper', sa.Float(), nullable=False),
        sa.Column('confidence_level', sa.Float(), nullable=True),
        sa.Column('model_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('model_version', sa.String(length=50), nullable=False),
        sa.Column('algorithm', sa.String(length=50), nullable=True),
        sa.Column('business_type', sa.String(length=50), nullable=True),
        sa.Column('day_of_week', sa.Integer(), nullable=False),
        sa.Column('is_holiday', sa.Boolean(), nullable=True),
        sa.Column('is_weekend', sa.Boolean(), nullable=True),
        sa.Column('weather_temperature', sa.Float(), nullable=True),
        sa.Column('weather_precipitation', sa.Float(), nullable=True),
        sa.Column('weather_description', sa.String(length=100), nullable=True),
        sa.Column('traffic_volume', sa.Integer(), nullable=True),
        sa.Column('processing_time_ms', sa.Integer(), nullable=True),
        sa.Column('features_used', sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    
    # Create indexes
    op.create_index('ix_forecasts_tenant_id', 'forecasts', ['tenant_id'])
    op.create_index('ix_forecasts_product_name', 'forecasts', ['product_name'])
    op.create_index('ix_forecasts_location', 'forecasts', ['location'])
    op.create_index('ix_forecasts_forecast_date', 'forecasts', ['forecast_date'])
    
    # Create prediction_batches table
    op.create_table('prediction_batches',
        sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('batch_name', sa.String(length=255), nullable=False),
        sa.Column('requested_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('status', sa.String(length=50), nullable=True),
        sa.Column('total_products', sa.Integer(), nullable=True),
        sa.Column('completed_products', sa.Integer(), nullable=True),
        sa.Column('failed_products', sa.Integer(), nullable=True),
        sa.Column('forecast_days', sa.Integer(), nullable=True),
        sa.Column('business_type', sa.String(length=50), nullable=True),
        sa.Column('error_message', sa.Text(), nullable=True),
        sa.Column('processing_time_ms', sa.Integer(), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    
    op.create_index('ix_prediction_batches_tenant_id', 'prediction_batches', ['tenant_id'])
    
    # Create forecast_alerts table
    op.create_table('forecast_alerts',
        sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('forecast_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('alert_type', sa.String(length=50), nullable=False),
        sa.Column('severity', sa.String(length=20), nullable=True),
        sa.Column('message', sa.Text(), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('acknowledged_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('resolved_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=True),
        sa.Column('notification_sent', sa.Boolean(), nullable=True),
        sa.Column('notification_method', sa.String(length=50), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    
    op.create_index('ix_forecast_alerts_tenant_id', 'forecast_alerts', ['tenant_id'])

def downgrade():
    op.drop_table('forecast_alerts')
    op.drop_table('prediction_batches')
    op.drop_table('forecasts')
