"""unified_initial_schema

Revision ID: 00001
Revises:
Create Date: 2025-11-10 19:00:00.000000+01:00

Complete unified initial schema for External Service including:
- Weather data collection (weather_data, weather_forecasts, city_weather_data)
- Traffic data collection (traffic_data, traffic_measurement_points, traffic_background_jobs, city_traffic_data)
- School calendars and location context (school_calendars, tenant_location_contexts)
- POI detection system (tenant_poi_contexts, poi_refresh_jobs)
- Audit logging (audit_logs)
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID, JSONB


# revision identifiers, used by Alembic.
revision: str = '00001'
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 all tables for External Service"""

    # ============================================================================
    # AUDIT LOGS
    # ============================================================================
    op.create_table(
        'audit_logs',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
        sa.Column('user_id', UUID(as_uuid=True), 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', JSONB, nullable=True),
        sa.Column('audit_metadata', JSONB, 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'])
    op.create_index('idx_audit_service_created', 'audit_logs', ['service_name', 'created_at'])
    op.create_index('idx_audit_severity_created', 'audit_logs', ['severity', 'created_at'])
    op.create_index('idx_audit_tenant_created', 'audit_logs', ['tenant_id', 'created_at'])
    op.create_index('idx_audit_user_created', 'audit_logs', ['user_id', 'created_at'])
    op.create_index(op.f('ix_audit_logs_action'), 'audit_logs', ['action'])
    op.create_index(op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'])
    op.create_index(op.f('ix_audit_logs_resource_id'), 'audit_logs', ['resource_id'])
    op.create_index(op.f('ix_audit_logs_resource_type'), 'audit_logs', ['resource_type'])
    op.create_index(op.f('ix_audit_logs_service_name'), 'audit_logs', ['service_name'])
    op.create_index(op.f('ix_audit_logs_severity'), 'audit_logs', ['severity'])
    op.create_index(op.f('ix_audit_logs_tenant_id'), 'audit_logs', ['tenant_id'])
    op.create_index(op.f('ix_audit_logs_user_id'), 'audit_logs', ['user_id'])

    # ============================================================================
    # WEATHER DATA
    # ============================================================================
    op.create_table(
        'city_weather_data',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('city_id', sa.String(length=50), nullable=False),
        sa.Column('date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('temperature', sa.Float(), nullable=True),
        sa.Column('precipitation', sa.Float(), nullable=True),
        sa.Column('humidity', sa.Float(), nullable=True),
        sa.Column('wind_speed', sa.Float(), nullable=True),
        sa.Column('pressure', sa.Float(), nullable=True),
        sa.Column('description', sa.String(length=200), nullable=True),
        sa.Column('source', sa.String(length=50), nullable=False),
        sa.Column('raw_data', JSONB, nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_city_weather_lookup', 'city_weather_data', ['city_id', 'date'])
    op.create_index(op.f('ix_city_weather_data_city_id'), 'city_weather_data', ['city_id'])
    op.create_index(op.f('ix_city_weather_data_date'), 'city_weather_data', ['date'])

    op.create_table(
        'weather_data',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('location_id', sa.String(length=100), nullable=False),
        sa.Column('city', sa.String(length=50), nullable=False),
        sa.Column('station_name', sa.String(length=200), nullable=True),
        sa.Column('latitude', sa.Float(), nullable=True),
        sa.Column('longitude', sa.Float(), nullable=True),
        sa.Column('date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('forecast_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('temperature', sa.Float(), nullable=True),
        sa.Column('temperature_min', sa.Float(), nullable=True),
        sa.Column('temperature_max', sa.Float(), nullable=True),
        sa.Column('feels_like', sa.Float(), nullable=True),
        sa.Column('precipitation', sa.Float(), nullable=True),
        sa.Column('precipitation_probability', sa.Float(), nullable=True),
        sa.Column('humidity', sa.Float(), nullable=True),
        sa.Column('wind_speed', sa.Float(), nullable=True),
        sa.Column('wind_direction', sa.Float(), nullable=True),
        sa.Column('wind_gust', sa.Float(), nullable=True),
        sa.Column('pressure', sa.Float(), nullable=True),
        sa.Column('visibility', sa.Float(), nullable=True),
        sa.Column('uv_index', sa.Float(), nullable=True),
        sa.Column('cloud_cover', sa.Float(), nullable=True),
        sa.Column('condition', sa.String(length=100), nullable=True),
        sa.Column('description', sa.String(length=200), nullable=True),
        sa.Column('weather_code', sa.String(length=20), nullable=True),
        sa.Column('source', sa.String(length=50), nullable=False),
        sa.Column('data_type', sa.String(length=20), nullable=False),
        sa.Column('is_forecast', sa.Boolean(), nullable=True),
        sa.Column('data_quality_score', sa.Float(), nullable=True),
        sa.Column('raw_data', JSONB, nullable=True),
        sa.Column('processed_data', JSONB, nullable=True),
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_weather_location_date', 'weather_data', ['location_id', 'date'])
    op.create_index(op.f('ix_weather_data_date'), 'weather_data', ['date'])
    op.create_index(op.f('ix_weather_data_location_id'), 'weather_data', ['location_id'])
    op.create_index(op.f('ix_weather_data_tenant_id'), 'weather_data', ['tenant_id'])

    op.create_table(
        'weather_forecasts',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('location_id', sa.String(length=100), nullable=False),
        sa.Column('forecast_date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('generated_at', sa.DateTime(timezone=True), nullable=False),
        sa.Column('temperature', sa.Float(), nullable=True),
        sa.Column('precipitation', sa.Float(), nullable=True),
        sa.Column('humidity', sa.Float(), nullable=True),
        sa.Column('wind_speed', sa.Float(), nullable=True),
        sa.Column('description', sa.String(length=200), nullable=True),
        sa.Column('source', sa.String(length=50), nullable=False),
        sa.Column('raw_data', sa.Text(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_forecast_location_date', 'weather_forecasts', ['location_id', 'forecast_date'])
    op.create_index(op.f('ix_weather_forecasts_location_id'), 'weather_forecasts', ['location_id'])

    # ============================================================================
    # TRAFFIC DATA
    # ============================================================================
    op.create_table(
        'city_traffic_data',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('city_id', sa.String(length=50), nullable=False),
        sa.Column('date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('traffic_volume', sa.Integer(), nullable=True),
        sa.Column('pedestrian_count', sa.Integer(), nullable=True),
        sa.Column('congestion_level', sa.String(length=20), nullable=True),
        sa.Column('average_speed', sa.Float(), nullable=True),
        sa.Column('source', sa.String(length=50), nullable=False),
        sa.Column('raw_data', JSONB, nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_city_traffic_lookup', 'city_traffic_data', ['city_id', 'date'])
    op.create_index(op.f('ix_city_traffic_data_city_id'), 'city_traffic_data', ['city_id'])
    op.create_index(op.f('ix_city_traffic_data_date'), 'city_traffic_data', ['date'])

    op.create_table(
        'traffic_measurement_points',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('city', sa.String(length=50), nullable=False),
        sa.Column('measurement_point_id', sa.String(length=100), nullable=False),
        sa.Column('name', sa.String(length=500), nullable=True),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('latitude', sa.Float(), nullable=False),
        sa.Column('longitude', sa.Float(), nullable=False),
        sa.Column('district', sa.String(length=100), nullable=True),
        sa.Column('zone', sa.String(length=100), nullable=True),
        sa.Column('road_type', sa.String(length=50), nullable=True),
        sa.Column('measurement_type', sa.String(length=50), nullable=True),
        sa.Column('point_category', sa.String(length=50), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=True),
        sa.Column('installation_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('last_data_received', sa.DateTime(timezone=True), nullable=True),
        sa.Column('data_quality_rating', sa.Float(), nullable=True),
        sa.Column('city_specific_metadata', JSONB, nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_points_active', 'traffic_measurement_points', ['city', 'is_active', 'last_data_received'])
    op.create_index('idx_points_city_location', 'traffic_measurement_points', ['city', 'latitude', 'longitude'])
    op.create_index('idx_points_district', 'traffic_measurement_points', ['city', 'district'])
    op.create_index('idx_points_road_type', 'traffic_measurement_points', ['city', 'road_type'])
    op.create_index('idx_unique_city_point', 'traffic_measurement_points', ['city', 'measurement_point_id'], unique=True)
    op.create_index(op.f('ix_traffic_measurement_points_city'), 'traffic_measurement_points', ['city'])
    op.create_index(op.f('ix_traffic_measurement_points_measurement_point_id'), 'traffic_measurement_points', ['measurement_point_id'])

    op.create_table(
        'traffic_data',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('location_id', sa.String(length=100), nullable=False),
        sa.Column('city', sa.String(length=50), nullable=False),
        sa.Column('date', sa.DateTime(timezone=True), nullable=False),
        sa.Column('traffic_volume', sa.Integer(), nullable=True),
        sa.Column('congestion_level', sa.String(length=20), nullable=True),
        sa.Column('average_speed', sa.Float(), nullable=True),
        sa.Column('occupation_percentage', sa.Float(), nullable=True),
        sa.Column('load_percentage', sa.Float(), nullable=True),
        sa.Column('pedestrian_count', sa.Integer(), nullable=True),
        sa.Column('measurement_point_id', sa.String(length=100), nullable=True),
        sa.Column('measurement_point_name', sa.String(length=500), nullable=True),
        sa.Column('measurement_point_type', sa.String(length=50), nullable=True),
        sa.Column('latitude', sa.Float(), nullable=True),
        sa.Column('longitude', sa.Float(), nullable=True),
        sa.Column('district', sa.String(length=100), nullable=True),
        sa.Column('zone', sa.String(length=100), nullable=True),
        sa.Column('source', sa.String(length=50), nullable=False),
        sa.Column('data_quality_score', sa.Float(), nullable=True),
        sa.Column('is_synthetic', sa.Boolean(), nullable=True),
        sa.Column('has_pedestrian_inference', sa.Boolean(), nullable=True),
        sa.Column('city_specific_data', JSONB, nullable=True),
        sa.Column('raw_data', sa.Text(), nullable=True),
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_traffic_city_date', 'traffic_data', ['city', 'date'])
    op.create_index('idx_traffic_city_location', 'traffic_data', ['city', 'location_id'])
    op.create_index('idx_traffic_district_date', 'traffic_data', ['city', 'district', 'date'])
    op.create_index('idx_traffic_location_date', 'traffic_data', ['location_id', 'date'])
    op.create_index('idx_traffic_measurement_point', 'traffic_data', ['city', 'measurement_point_id'])
    op.create_index('idx_traffic_quality', 'traffic_data', ['city', 'data_quality_score', 'date'])
    op.create_index('idx_traffic_tenant_date', 'traffic_data', ['tenant_id', 'date'])
    op.create_index('idx_traffic_training', 'traffic_data', ['tenant_id', 'city', 'date', 'is_synthetic'])
    op.create_index(op.f('ix_traffic_data_city'), 'traffic_data', ['city'])
    op.create_index(op.f('ix_traffic_data_date'), 'traffic_data', ['date'])
    op.create_index(op.f('ix_traffic_data_location_id'), 'traffic_data', ['location_id'])
    op.create_index(op.f('ix_traffic_data_measurement_point_id'), 'traffic_data', ['measurement_point_id'])
    op.create_index(op.f('ix_traffic_data_tenant_id'), 'traffic_data', ['tenant_id'])

    op.create_table(
        'traffic_background_jobs',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('job_type', sa.String(length=50), nullable=False),
        sa.Column('city', sa.String(length=50), nullable=False),
        sa.Column('location_pattern', sa.String(length=200), nullable=True),
        sa.Column('scheduled_at', sa.DateTime(timezone=True), nullable=False),
        sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('status', sa.String(length=20), nullable=False),
        sa.Column('progress_percentage', sa.Float(), nullable=True),
        sa.Column('records_processed', sa.Integer(), nullable=True),
        sa.Column('records_stored', sa.Integer(), nullable=True),
        sa.Column('data_start_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('data_end_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('success_count', sa.Integer(), nullable=True),
        sa.Column('error_count', sa.Integer(), nullable=True),
        sa.Column('error_message', sa.Text(), nullable=True),
        sa.Column('job_metadata', JSONB, nullable=True),
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_jobs_city_status', 'traffic_background_jobs', ['city', 'status', 'scheduled_at'])
    op.create_index('idx_jobs_completed', 'traffic_background_jobs', ['status', 'completed_at'])
    op.create_index('idx_jobs_tenant_status', 'traffic_background_jobs', ['tenant_id', 'status', 'scheduled_at'])
    op.create_index('idx_jobs_type_city', 'traffic_background_jobs', ['job_type', 'city', 'scheduled_at'])
    op.create_index(op.f('ix_traffic_background_jobs_city'), 'traffic_background_jobs', ['city'])
    op.create_index(op.f('ix_traffic_background_jobs_tenant_id'), 'traffic_background_jobs', ['tenant_id'])

    # ============================================================================
    # SCHOOL CALENDARS & LOCATION CONTEXT
    # ============================================================================
    op.create_table(
        'school_calendars',
        sa.Column('id', UUID(as_uuid=True), nullable=False),
        sa.Column('city_id', sa.String(length=50), nullable=False),
        sa.Column('calendar_name', sa.String(length=100), nullable=False),
        sa.Column('school_type', sa.String(length=20), nullable=False),
        sa.Column('academic_year', sa.String(length=10), nullable=False),
        sa.Column('holiday_periods', JSONB, nullable=False),
        sa.Column('school_hours', JSONB, nullable=False),
        sa.Column('source', sa.String(length=100), nullable=True),
        sa.Column('enabled', sa.Boolean(), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_school_calendar_city_year', 'school_calendars', ['city_id', 'academic_year'])
    op.create_index('idx_school_calendar_city_type', 'school_calendars', ['city_id', 'school_type'])
    op.create_index(op.f('ix_school_calendars_city_id'), 'school_calendars', ['city_id'])

    op.create_table(
        'tenant_location_contexts',
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
        sa.Column('city_id', sa.String(length=50), nullable=False),
        sa.Column('school_calendar_id', UUID(as_uuid=True), nullable=True),
        sa.Column('neighborhood', sa.String(length=100), nullable=True),
        sa.Column('local_events', JSONB, nullable=True),
        sa.Column('notes', sa.String(length=500), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.PrimaryKeyConstraint('tenant_id')
    )
    op.create_index('idx_tenant_location_calendar', 'tenant_location_contexts', ['school_calendar_id'])
    op.create_index(op.f('ix_tenant_location_contexts_city_id'), 'tenant_location_contexts', ['city_id'])

    # ============================================================================
    # POI DETECTION SYSTEM
    # ============================================================================
    op.create_table(
        'tenant_poi_contexts',
        sa.Column('id', UUID(as_uuid=True), primary_key=True),
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=False, unique=True, index=True),
        sa.Column('latitude', sa.Float(), nullable=False),
        sa.Column('longitude', sa.Float(), nullable=False),
        sa.Column('poi_detection_results', JSONB, nullable=False, server_default='{}'),
        sa.Column('ml_features', JSONB, nullable=False, server_default='{}'),
        sa.Column('total_pois_detected', sa.Integer(), default=0),
        sa.Column('high_impact_categories', JSONB, server_default='[]'),
        sa.Column('relevant_categories', JSONB, server_default='[]'),
        sa.Column('detection_timestamp', sa.DateTime(timezone=True), nullable=False),
        sa.Column('detection_source', sa.String(50), default='overpass_api'),
        sa.Column('detection_status', sa.String(20), default='completed'),
        sa.Column('detection_error', sa.String(500), nullable=True),
        sa.Column('next_refresh_date', sa.DateTime(timezone=True), nullable=True),
        sa.Column('refresh_interval_days', sa.Integer(), default=180),
        sa.Column('last_refreshed_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now())
    )
    op.create_index('idx_tenant_poi_location', 'tenant_poi_contexts', ['latitude', 'longitude'])
    op.create_index('idx_tenant_poi_refresh', 'tenant_poi_contexts', ['next_refresh_date'])
    op.create_index('idx_tenant_poi_status', 'tenant_poi_contexts', ['detection_status'])

    op.create_table(
        'poi_refresh_jobs',
        sa.Column('id', UUID(as_uuid=True), primary_key=True),
        sa.Column('tenant_id', UUID(as_uuid=True), nullable=False, index=True),
        sa.Column('scheduled_at', sa.DateTime(timezone=True), nullable=False, index=True),
        sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('status', sa.String(50), nullable=False, default='pending', index=True),
        sa.Column('attempt_count', sa.Integer, nullable=False, default=0),
        sa.Column('max_attempts', sa.Integer, nullable=False, default=3),
        sa.Column('latitude', sa.Float, nullable=False),
        sa.Column('longitude', sa.Float, nullable=False),
        sa.Column('pois_detected', sa.Integer, nullable=True),
        sa.Column('changes_detected', sa.Boolean, default=False),
        sa.Column('change_summary', JSONB, nullable=True),
        sa.Column('error_message', sa.Text, nullable=True),
        sa.Column('error_details', JSONB, nullable=True),
        sa.Column('next_scheduled_at', sa.DateTime(timezone=True), nullable=True, index=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now(), onupdate=sa.func.now())
    )
    op.create_index('idx_poi_refresh_jobs_tenant_status', 'poi_refresh_jobs', ['tenant_id', 'status'])
    op.create_index('idx_poi_refresh_jobs_status_scheduled', 'poi_refresh_jobs', ['status', 'scheduled_at'])


def downgrade() -> None:
    """Drop all tables"""

    # POI Detection System
    op.drop_index('idx_poi_refresh_jobs_status_scheduled', table_name='poi_refresh_jobs')
    op.drop_index('idx_poi_refresh_jobs_tenant_status', table_name='poi_refresh_jobs')
    op.drop_table('poi_refresh_jobs')

    op.drop_index('idx_tenant_poi_status', table_name='tenant_poi_contexts')
    op.drop_index('idx_tenant_poi_refresh', table_name='tenant_poi_contexts')
    op.drop_index('idx_tenant_poi_location', table_name='tenant_poi_contexts')
    op.drop_table('tenant_poi_contexts')

    # School Calendars & Location Context
    op.drop_index(op.f('ix_tenant_location_contexts_city_id'), table_name='tenant_location_contexts')
    op.drop_index('idx_tenant_location_calendar', table_name='tenant_location_contexts')
    op.drop_table('tenant_location_contexts')

    op.drop_index(op.f('ix_school_calendars_city_id'), table_name='school_calendars')
    op.drop_index('idx_school_calendar_city_type', table_name='school_calendars')
    op.drop_index('idx_school_calendar_city_year', table_name='school_calendars')
    op.drop_table('school_calendars')

    # Traffic Data
    op.drop_index(op.f('ix_traffic_background_jobs_tenant_id'), table_name='traffic_background_jobs')
    op.drop_index(op.f('ix_traffic_background_jobs_city'), table_name='traffic_background_jobs')
    op.drop_index('idx_jobs_type_city', table_name='traffic_background_jobs')
    op.drop_index('idx_jobs_tenant_status', table_name='traffic_background_jobs')
    op.drop_index('idx_jobs_completed', table_name='traffic_background_jobs')
    op.drop_index('idx_jobs_city_status', table_name='traffic_background_jobs')
    op.drop_table('traffic_background_jobs')

    op.drop_index(op.f('ix_traffic_data_tenant_id'), table_name='traffic_data')
    op.drop_index(op.f('ix_traffic_data_measurement_point_id'), table_name='traffic_data')
    op.drop_index(op.f('ix_traffic_data_location_id'), table_name='traffic_data')
    op.drop_index(op.f('ix_traffic_data_date'), table_name='traffic_data')
    op.drop_index(op.f('ix_traffic_data_city'), table_name='traffic_data')
    op.drop_index('idx_traffic_training', table_name='traffic_data')
    op.drop_index('idx_traffic_tenant_date', table_name='traffic_data')
    op.drop_index('idx_traffic_quality', table_name='traffic_data')
    op.drop_index('idx_traffic_measurement_point', table_name='traffic_data')
    op.drop_index('idx_traffic_location_date', table_name='traffic_data')
    op.drop_index('idx_traffic_district_date', table_name='traffic_data')
    op.drop_index('idx_traffic_city_location', table_name='traffic_data')
    op.drop_index('idx_traffic_city_date', table_name='traffic_data')
    op.drop_table('traffic_data')

    op.drop_index(op.f('ix_traffic_measurement_points_measurement_point_id'), table_name='traffic_measurement_points')
    op.drop_index(op.f('ix_traffic_measurement_points_city'), table_name='traffic_measurement_points')
    op.drop_index('idx_unique_city_point', table_name='traffic_measurement_points')
    op.drop_index('idx_points_road_type', table_name='traffic_measurement_points')
    op.drop_index('idx_points_district', table_name='traffic_measurement_points')
    op.drop_index('idx_points_city_location', table_name='traffic_measurement_points')
    op.drop_index('idx_points_active', table_name='traffic_measurement_points')
    op.drop_table('traffic_measurement_points')

    op.drop_index(op.f('ix_city_traffic_data_date'), table_name='city_traffic_data')
    op.drop_index(op.f('ix_city_traffic_data_city_id'), table_name='city_traffic_data')
    op.drop_index('idx_city_traffic_lookup', table_name='city_traffic_data')
    op.drop_table('city_traffic_data')

    # Weather Data
    op.drop_index(op.f('ix_weather_forecasts_location_id'), table_name='weather_forecasts')
    op.drop_index('idx_forecast_location_date', table_name='weather_forecasts')
    op.drop_table('weather_forecasts')

    op.drop_index(op.f('ix_weather_data_tenant_id'), table_name='weather_data')
    op.drop_index(op.f('ix_weather_data_location_id'), table_name='weather_data')
    op.drop_index(op.f('ix_weather_data_date'), table_name='weather_data')
    op.drop_index('idx_weather_location_date', table_name='weather_data')
    op.drop_table('weather_data')

    op.drop_index(op.f('ix_city_weather_data_date'), table_name='city_weather_data')
    op.drop_index(op.f('ix_city_weather_data_city_id'), table_name='city_weather_data')
    op.drop_index('idx_city_weather_lookup', table_name='city_weather_data')
    op.drop_table('city_weather_data')

    # Audit Logs
    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.drop_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')
