"""Initial baseline v2

Revision ID: 13d050e8cf6d
Revises: 13bd03551ebf
Create Date: 2026-01-26 09:53:50.248698

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '13d050e8cf6d'
down_revision: Union[str, Sequence[str], None] = '13bd03551ebf'
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('email_logs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('email', sa.String(), nullable=True),
    sa.Column('type', sa.String(), nullable=True),
    sa.Column('sent_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_email_logs_email'), 'email_logs', ['email'], unique=False, schema='data')
    op.create_index(op.f('ix_data_email_logs_id'), 'email_logs', ['id'], unique=False, schema='data')
    op.create_table('email_provider_configs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=True),
    sa.Column('provider_type', sa.String(length=20), nullable=True),
    sa.Column('priority', sa.Integer(), nullable=True),
    sa.Column('settings', sa.JSON(), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=True),
    sa.Column('fail_count', sa.Integer(), nullable=True),
    sa.Column('max_fail_threshold', sa.Integer(), nullable=True),
    sa.Column('success_rate', sa.Float(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name'),
    schema='data'
    )
    op.create_index(op.f('ix_data_email_provider_configs_id'), 'email_provider_configs', ['id'], unique=False, schema='data')
    op.create_table('email_templates',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('type', sa.Enum('REGISTRATION', 'PASSWORD_RESET', 'GDPR_NOTICE', name='emailtype'), nullable=True),
    sa.Column('subject', sa.String(length=255), nullable=False),
    sa.Column('body_html', sa.Text(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_email_templates_id'), 'email_templates', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_email_templates_type'), 'email_templates', ['type'], unique=True, schema='data')
    op.create_table('legal_documents',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('title', sa.String(length=255), nullable=True),
    sa.Column('content', sa.Text(), nullable=False),
    sa.Column('version', sa.String(length=20), nullable=False),
    sa.Column('region_code', sa.String(length=5), nullable=True),
    sa.Column('language', sa.String(length=5), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_legal_documents_id'), 'legal_documents', ['id'], unique=False, schema='data')
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('password_hash', sa.Text(), nullable=False),
    sa.Column('first_name', sa.String(), nullable=True),
    sa.Column('last_name', sa.String(), nullable=True),
    sa.Column('birthday', sa.Date(), nullable=True),
    sa.Column('is_email_verified', sa.Boolean(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=True),
    sa.Column('is_banned', sa.Boolean(), nullable=True),
    sa.Column('is_gdpr_deleted', sa.Boolean(), nullable=True),
    sa.Column('previous_login_count', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.Column('verified_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_users_email'), 'users', ['email'], unique=True, schema='data')
    op.create_index(op.f('ix_data_users_id'), 'users', ['id'], unique=False, schema='data')
    op.create_table('legal_acceptances',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('document_id', sa.Integer(), nullable=True),
    sa.Column('accepted_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.Column('ip_address', sa.String(length=45), nullable=True),
    sa.Column('user_agent', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['document_id'], ['data.legal_documents.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_legal_acceptances_id'), 'legal_acceptances', ['id'], unique=False, schema='data')
    op.create_table('organizations',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('org_type', sa.Enum('PRIVATE', 'COMPANY', name='orgtype'), nullable=True),
    sa.Column('tax_number', sa.String(), nullable=True),
    sa.Column('founded_at', sa.Date(), nullable=True),
    sa.Column('validation_status', sa.Enum('NOT_VALIDATED', 'PENDING', 'VALIDATED', 'REJECTED', name='validationstatus'), nullable=True),
    sa.Column('owner_id', sa.Integer(), nullable=True),
    sa.Column('ui_theme', sa.String(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.ForeignKeyConstraint(['owner_id'], ['data.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_organizations_id'), 'organizations', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_organizations_tax_number'), 'organizations', ['tax_number'], unique=False, schema='data')
    op.create_table('verification_tokens',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('token', sa.String(length=255), nullable=True),
    sa.Column('token_type', sa.Enum('EMAIL_VERIFY', 'PASSWORD_RESET', name='tokentype'), nullable=True),
    sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_verification_tokens_id'), 'verification_tokens', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_verification_tokens_token'), 'verification_tokens', ['token'], unique=True, schema='data')
    op.create_table('organization_members',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('org_id', sa.Integer(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('role', sa.Enum('OWNER', 'ADMIN', 'FLEET_MANAGER', 'DRIVER', name='userrole'), nullable=True),
    sa.Column('is_permanent', sa.Boolean(), nullable=True),
    sa.ForeignKeyConstraint(['org_id'], ['data.organizations.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_organization_members_id'), 'organization_members', ['id'], unique=False, schema='data')
    op.create_table('vehicles',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('vin', sa.String(), nullable=False),
    sa.Column('license_plate', sa.String(), nullable=False),
    sa.Column('make', sa.String(), nullable=True),
    sa.Column('model', sa.String(), nullable=True),
    sa.Column('year', sa.Integer(), nullable=True),
    sa.Column('is_deleted', sa.Boolean(), nullable=True),
    sa.Column('current_org_id', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.ForeignKeyConstraint(['current_org_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicles_id'), 'vehicles', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicles_license_plate'), 'vehicles', ['license_plate'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicles_vin'), 'vehicles', ['vin'], unique=True, schema='data')
    op.drop_table('alembic_version')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('alembic_version',
    sa.Column('version_num', sa.VARCHAR(length=32), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('version_num', name=op.f('alembic_version_pkc'))
    )
    op.drop_index(op.f('ix_data_vehicles_vin'), table_name='vehicles', schema='data')
    op.drop_index(op.f('ix_data_vehicles_license_plate'), table_name='vehicles', schema='data')
    op.drop_index(op.f('ix_data_vehicles_id'), table_name='vehicles', schema='data')
    op.drop_table('vehicles', schema='data')
    op.drop_index(op.f('ix_data_organization_members_id'), table_name='organization_members', schema='data')
    op.drop_table('organization_members', schema='data')
    op.drop_index(op.f('ix_data_verification_tokens_token'), table_name='verification_tokens', schema='data')
    op.drop_index(op.f('ix_data_verification_tokens_id'), table_name='verification_tokens', schema='data')
    op.drop_table('verification_tokens', schema='data')
    op.drop_index(op.f('ix_data_organizations_tax_number'), table_name='organizations', schema='data')
    op.drop_index(op.f('ix_data_organizations_id'), table_name='organizations', schema='data')
    op.drop_table('organizations', schema='data')
    op.drop_index(op.f('ix_data_legal_acceptances_id'), table_name='legal_acceptances', schema='data')
    op.drop_table('legal_acceptances', schema='data')
    op.drop_index(op.f('ix_data_users_id'), table_name='users', schema='data')
    op.drop_index(op.f('ix_data_users_email'), table_name='users', schema='data')
    op.drop_table('users', schema='data')
    op.drop_index(op.f('ix_data_legal_documents_id'), table_name='legal_documents', schema='data')
    op.drop_table('legal_documents', schema='data')
    op.drop_index(op.f('ix_data_email_templates_type'), table_name='email_templates', schema='data')
    op.drop_index(op.f('ix_data_email_templates_id'), table_name='email_templates', schema='data')
    op.drop_table('email_templates', schema='data')
    op.drop_index(op.f('ix_data_email_provider_configs_id'), table_name='email_provider_configs', schema='data')
    op.drop_table('email_provider_configs', schema='data')
    op.drop_index(op.f('ix_data_email_logs_id'), table_name='email_logs', schema='data')
    op.drop_index(op.f('ix_data_email_logs_email'), table_name='email_logs', schema='data')
    op.drop_table('email_logs', schema='data')
    # ### end Alembic commands ###
