"""initial_schema_20251015_1228

Revision ID: 1949ed96e20e
Revises: 
Create Date: 2025-10-15 12:28:44.373103+02:00

"""
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 = '1949ed96e20e'
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:
    # ### commands auto generated by Alembic - please adjust! ###
    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)
    op.create_table('sales_data',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('tenant_id', sa.UUID(), nullable=False),
    sa.Column('date', sa.DateTime(timezone=True), nullable=False),
    sa.Column('inventory_product_id', sa.UUID(), nullable=False),
    sa.Column('quantity_sold', sa.Integer(), nullable=False),
    sa.Column('unit_price', sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column('revenue', sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column('cost_of_goods', sa.Numeric(precision=10, scale=2), nullable=True),
    sa.Column('discount_applied', sa.Numeric(precision=5, scale=2), nullable=True),
    sa.Column('location_id', sa.String(length=100), nullable=True),
    sa.Column('sales_channel', sa.String(length=50), nullable=True),
    sa.Column('source', sa.String(length=50), nullable=False),
    sa.Column('is_validated', sa.Boolean(), nullable=True),
    sa.Column('validation_notes', sa.Text(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('weather_condition', sa.String(length=50), nullable=True),
    sa.Column('is_holiday', sa.Boolean(), nullable=True),
    sa.Column('is_weekend', sa.Boolean(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_by', sa.UUID(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_sales_channel_date', 'sales_data', ['sales_channel', 'date', 'tenant_id'], unique=False)
    op.create_index('idx_sales_date_range', 'sales_data', ['date', 'tenant_id'], unique=False)
    op.create_index('idx_sales_inventory_product', 'sales_data', ['inventory_product_id', 'tenant_id'], unique=False)
    op.create_index('idx_sales_product_date', 'sales_data', ['inventory_product_id', 'date', 'tenant_id'], unique=False)
    op.create_index('idx_sales_source_validated', 'sales_data', ['source', 'is_validated', 'tenant_id'], unique=False)
    op.create_index('idx_sales_tenant_date', 'sales_data', ['tenant_id', 'date'], unique=False)
    op.create_index('idx_sales_tenant_location', 'sales_data', ['tenant_id', 'location_id'], unique=False)
    op.create_index(op.f('ix_sales_data_date'), 'sales_data', ['date'], unique=False)
    op.create_index(op.f('ix_sales_data_inventory_product_id'), 'sales_data', ['inventory_product_id'], unique=False)
    op.create_index(op.f('ix_sales_data_location_id'), 'sales_data', ['location_id'], unique=False)
    op.create_index(op.f('ix_sales_data_tenant_id'), 'sales_data', ['tenant_id'], unique=False)
    op.create_table('sales_import_jobs',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('tenant_id', sa.UUID(), nullable=False),
    sa.Column('filename', sa.String(length=255), nullable=False),
    sa.Column('file_size', sa.Integer(), nullable=True),
    sa.Column('import_type', sa.String(length=50), nullable=False),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('progress_percentage', sa.Float(), nullable=True),
    sa.Column('total_rows', sa.Integer(), nullable=True),
    sa.Column('processed_rows', sa.Integer(), nullable=True),
    sa.Column('successful_imports', sa.Integer(), nullable=True),
    sa.Column('failed_imports', sa.Integer(), nullable=True),
    sa.Column('duplicate_rows', sa.Integer(), nullable=True),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('validation_errors', sa.Text(), nullable=True),
    sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_by', sa.UUID(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_import_jobs_status_date', 'sales_import_jobs', ['status', 'created_at'], unique=False)
    op.create_index('idx_import_jobs_tenant_status', 'sales_import_jobs', ['tenant_id', 'status', 'created_at'], unique=False)
    op.create_index(op.f('ix_sales_import_jobs_tenant_id'), 'sales_import_jobs', ['tenant_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_sales_import_jobs_tenant_id'), table_name='sales_import_jobs')
    op.drop_index('idx_import_jobs_tenant_status', table_name='sales_import_jobs')
    op.drop_index('idx_import_jobs_status_date', table_name='sales_import_jobs')
    op.drop_table('sales_import_jobs')
    op.drop_index(op.f('ix_sales_data_tenant_id'), table_name='sales_data')
    op.drop_index(op.f('ix_sales_data_location_id'), table_name='sales_data')
    op.drop_index(op.f('ix_sales_data_inventory_product_id'), table_name='sales_data')
    op.drop_index(op.f('ix_sales_data_date'), table_name='sales_data')
    op.drop_index('idx_sales_tenant_location', table_name='sales_data')
    op.drop_index('idx_sales_tenant_date', table_name='sales_data')
    op.drop_index('idx_sales_source_validated', table_name='sales_data')
    op.drop_index('idx_sales_product_date', table_name='sales_data')
    op.drop_index('idx_sales_inventory_product', table_name='sales_data')
    op.drop_index('idx_sales_date_range', table_name='sales_data')
    op.drop_index('idx_sales_channel_date', table_name='sales_data')
    op.drop_table('sales_data')
    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')
    # ### end Alembic commands ###
