"""MB2_Genesis_Final

Revision ID: 78f5b29d0714
Revises: 
Create Date: 2026-02-23 23:33:45.271156

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = '78f5b29d0714'
down_revision: Union[str, Sequence[str], None] = None
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.execute('CREATE EXTENSION IF NOT EXISTS postgis')
    op.execute('CREATE SCHEMA IF NOT EXISTS identity')
    op.execute('CREATE SCHEMA IF NOT EXISTS data')
    op.execute('CREATE SCHEMA IF NOT EXISTS system')
    op.execute('CREATE EXTENSION IF NOT EXISTS postgis')
    op.create_table('badges',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=False),
    sa.Column('icon_url', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name'),
    schema='data'
    )
    op.create_index(op.f('ix_data_badges_id'), 'badges', ['id'], unique=False, schema='data')
    op.create_table('catalog_discovery',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('make', sa.String(length=100), nullable=False),
    sa.Column('model', sa.String(length=100), nullable=False),
    sa.Column('vehicle_class', sa.String(length=50), nullable=True),
    sa.Column('source', sa.String(length=50), nullable=True),
    sa.Column('status', sa.String(length=20), server_default=sa.text("'pending'"), nullable=False),
    sa.Column('attempts', sa.Integer(), nullable=False),
    sa.Column('last_attempt', sa.DateTime(timezone=True), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('make', 'model', 'vehicle_class', name='_make_model_class_uc'),
    schema='data'
    )
    op.create_index(op.f('ix_data_catalog_discovery_id'), 'catalog_discovery', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_catalog_discovery_make'), 'catalog_discovery', ['make'], unique=False, schema='data')
    op.create_index(op.f('ix_data_catalog_discovery_model'), 'catalog_discovery', ['model'], unique=False, schema='data')
    op.create_index(op.f('ix_data_catalog_discovery_status'), 'catalog_discovery', ['status'], unique=False, schema='data')
    op.create_index(op.f('ix_data_catalog_discovery_vehicle_class'), 'catalog_discovery', ['vehicle_class'], unique=False, schema='data')
    op.create_table('discovery_parameters',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('city', sa.String(length=100), nullable=False),
    sa.Column('keyword', sa.String(length=100), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('last_run_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('exchange_rates',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('base_currency', sa.String(length=3), nullable=False),
    sa.Column('target_currency', sa.String(length=3), nullable=True),
    sa.Column('rate', sa.Numeric(precision=18, scale=6), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('target_currency'),
    schema='data'
    )
    op.create_table('expertise_tags',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('key', sa.String(length=50), nullable=False),
    sa.Column('name_hu', sa.String(length=100), nullable=True),
    sa.Column('category', sa.String(length=30), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_expertise_tags_key'), 'expertise_tags', ['key'], unique=True, schema='data')
    op.create_table('geo_postal_codes',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('country_code', sa.String(length=5), nullable=False),
    sa.Column('zip_code', sa.String(length=10), nullable=False),
    sa.Column('city', sa.String(length=100), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_geo_postal_codes_city'), 'geo_postal_codes', ['city'], unique=False, schema='data')
    op.create_index(op.f('ix_data_geo_postal_codes_zip_code'), 'geo_postal_codes', ['zip_code'], unique=False, schema='data')
    op.create_table('geo_street_types',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name'),
    schema='data'
    )
    op.create_table('level_configs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('level_number', sa.Integer(), nullable=False),
    sa.Column('min_points', sa.Integer(), nullable=False),
    sa.Column('rank_name', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('level_number'),
    schema='data'
    )
    op.create_index(op.f('ix_data_level_configs_id'), 'level_configs', ['id'], unique=False, schema='data')
    op.create_table('point_rules',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('action_key', sa.String(), nullable=False),
    sa.Column('points', sa.Integer(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_point_rules_action_key'), 'point_rules', ['action_key'], unique=True, schema='data')
    op.create_index(op.f('ix_data_point_rules_id'), 'point_rules', ['id'], unique=False, schema='data')
    op.create_table('service_specialties',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('parent_id', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('slug', sa.String(), nullable=False),
    sa.ForeignKeyConstraint(['parent_id'], ['data.service_specialties.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_service_specialties_slug'), 'service_specialties', ['slug'], unique=True, schema='data')
    op.create_table('service_staging',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('postal_code', sa.String(length=10), nullable=True),
    sa.Column('city', sa.String(length=100), nullable=True),
    sa.Column('full_address', sa.String(), nullable=True),
    sa.Column('fingerprint', sa.String(length=255), nullable=False),
    sa.Column('raw_data', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('status', sa.String(length=20), server_default=sa.text("'pending'"), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index('idx_staging_fingerprint', 'service_staging', ['fingerprint'], unique=True, schema='data')
    op.create_index(op.f('ix_data_service_staging_city'), 'service_staging', ['city'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_staging_id'), 'service_staging', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_staging_name'), 'service_staging', ['name'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_staging_postal_code'), 'service_staging', ['postal_code'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_staging_status'), 'service_staging', ['status'], unique=False, schema='data')
    op.create_table('subscription_tiers',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('rules', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('is_custom', sa.Boolean(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_subscription_tiers_name'), 'subscription_tiers', ['name'], unique=True, schema='data')
    op.create_table('translations',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('key', sa.String(length=255), nullable=False),
    sa.Column('lang', sa.String(length=5), nullable=False),
    sa.Column('value', sa.Text(), nullable=False),
    sa.Column('is_published', sa.Boolean(), server_default=sa.text('true'), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_translations_id'), 'translations', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_translations_key'), 'translations', ['key'], unique=False, schema='data')
    op.create_index(op.f('ix_data_translations_lang'), 'translations', ['lang'], unique=False, schema='data')
    op.create_table('vehicle_types',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('code', sa.String(length=30), nullable=False),
    sa.Column('name', sa.String(length=50), nullable=False),
    sa.Column('icon', sa.String(length=50), nullable=True),
    sa.Column('units', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text('\'{"power": "kW", "weight": "kg"}\'::jsonb'), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicle_types_code'), 'vehicle_types', ['code'], unique=True, schema='data')
    op.create_table('addresses',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('postal_code_id', sa.Integer(), nullable=True),
    sa.Column('street_name', sa.String(length=200), nullable=False),
    sa.Column('street_type', sa.String(length=50), nullable=False),
    sa.Column('house_number', sa.String(length=50), nullable=False),
    sa.Column('stairwell', sa.String(length=20), nullable=True),
    sa.Column('floor', sa.String(length=20), nullable=True),
    sa.Column('door', sa.String(length=20), nullable=True),
    sa.Column('parcel_id', sa.String(length=50), nullable=True),
    sa.Column('full_address_text', sa.Text(), nullable=True),
    sa.Column('latitude', sa.Float(), nullable=True),
    sa.Column('longitude', sa.Float(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['postal_code_id'], ['data.geo_postal_codes.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('feature_definitions',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('vehicle_type_id', sa.Integer(), nullable=False),
    sa.Column('code', sa.String(length=50), nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('category', sa.String(length=50), nullable=False),
    sa.ForeignKeyConstraint(['vehicle_type_id'], ['data.vehicle_types.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_feature_definitions_category'), 'feature_definitions', ['category'], unique=False, schema='data')
    op.create_index(op.f('ix_data_feature_definitions_code'), 'feature_definitions', ['code'], unique=False, schema='data')
    op.create_table('geo_streets',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('postal_code_id', sa.Integer(), nullable=True),
    sa.Column('name', sa.String(length=200), nullable=False),
    sa.ForeignKeyConstraint(['postal_code_id'], ['data.geo_postal_codes.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_geo_streets_name'), 'geo_streets', ['name'], unique=False, schema='data')
    op.create_table('vehicle_model_definitions',
    sa.Column('raw_search_context', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('make', sa.String(length=50), nullable=False),
    sa.Column('technical_code', sa.String(length=50), nullable=False),
    sa.Column('marketing_name', sa.String(length=100), nullable=True),
    sa.Column('vehicle_type_id', sa.Integer(), nullable=True),
    sa.Column('year_from', sa.Integer(), nullable=True),
    sa.Column('year_to', sa.Integer(), nullable=True),
    sa.Column('status', sa.String(length=30), server_default=sa.text("'active'"), nullable=False),
    sa.Column('is_manual', sa.Boolean(), nullable=False),
    sa.Column('attempts', sa.Integer(), nullable=False),
    sa.Column('research_metadata', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('specifications', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['vehicle_type_id'], ['data.vehicle_types.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('make', 'technical_code', 'vehicle_type_id', name='uix_make_tech_type'),
    schema='data'
    )
    op.create_index('idx_vmd_lookup', 'vehicle_model_definitions', ['make', 'technical_code'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_make'), 'vehicle_model_definitions', ['make'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_marketing_name'), 'vehicle_model_definitions', ['marketing_name'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_status'), 'vehicle_model_definitions', ['status'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_technical_code'), 'vehicle_model_definitions', ['technical_code'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_year_from'), 'vehicle_model_definitions', ['year_from'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_model_definitions_year_to'), 'vehicle_model_definitions', ['year_to'], unique=False, schema='data')
    op.create_table('model_feature_maps',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('model_definition_id', sa.Integer(), nullable=False),
    sa.Column('feature_id', sa.Integer(), nullable=False),
    sa.Column('is_standard', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['feature_id'], ['data.feature_definitions.id'], ),
    sa.ForeignKeyConstraint(['model_definition_id'], ['data.vehicle_model_definitions.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('vehicle_catalog',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('master_definition_id', sa.Integer(), nullable=True),
    sa.Column('make', sa.String(), nullable=False),
    sa.Column('model', sa.String(), nullable=False),
    sa.Column('generation', sa.String(), nullable=True),
    sa.Column('engine_variant', sa.String(), nullable=True),
    sa.Column('year_from', sa.Integer(), nullable=True),
    sa.Column('year_to', sa.Integer(), nullable=True),
    sa.Column('vehicle_class', sa.String(), nullable=True),
    sa.Column('fuel_type', sa.String(), nullable=True),
    sa.Column('power_kw', sa.Integer(), nullable=True),
    sa.Column('engine_capacity', sa.Integer(), nullable=True),
    sa.Column('max_weight_kg', sa.Integer(), nullable=True),
    sa.Column('axle_count', sa.Integer(), nullable=True),
    sa.Column('euro_class', sa.String(length=20), nullable=True),
    sa.Column('body_type', sa.String(length=100), nullable=True),
    sa.Column('engine_code', sa.String(), nullable=True),
    sa.Column('factory_data', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.ForeignKeyConstraint(['master_definition_id'], ['data.vehicle_model_definitions.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('make', 'model', 'year_from', 'engine_variant', 'fuel_type', name='uix_vehicle_catalog_full'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicle_catalog_engine_capacity'), 'vehicle_catalog', ['engine_capacity'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_engine_variant'), 'vehicle_catalog', ['engine_variant'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_fuel_type'), 'vehicle_catalog', ['fuel_type'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_generation'), 'vehicle_catalog', ['generation'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_id'), 'vehicle_catalog', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_make'), 'vehicle_catalog', ['make'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_model'), 'vehicle_catalog', ['model'], unique=False, schema='data')
    op.create_index(op.f('ix_data_vehicle_catalog_power_kw'), 'vehicle_catalog', ['power_kw'], unique=False, schema='data')
    op.create_table('persons',
    sa.Column('id', sa.BigInteger(), nullable=False),
    sa.Column('id_uuid', sa.UUID(), nullable=False),
    sa.Column('address_id', sa.UUID(), nullable=True),
    sa.Column('identity_hash', sa.String(length=64), nullable=True),
    sa.Column('last_name', sa.String(), nullable=False),
    sa.Column('first_name', sa.String(), nullable=False),
    sa.Column('phone', sa.String(), nullable=True),
    sa.Column('mothers_last_name', sa.String(), nullable=True),
    sa.Column('mothers_first_name', sa.String(), nullable=True),
    sa.Column('birth_place', sa.String(), nullable=True),
    sa.Column('birth_date', sa.DateTime(), nullable=True),
    sa.Column('identity_docs', sa.JSON(), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('ice_contact', sa.JSON(), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('lifetime_xp', sa.BigInteger(), server_default=sa.text('0'), nullable=False),
    sa.Column('penalty_points', sa.Integer(), server_default=sa.text('0'), nullable=False),
    sa.Column('social_reputation', sa.Numeric(precision=3, scale=2), server_default=sa.text('1.00'), nullable=False),
    sa.Column('is_sales_agent', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_ghost', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['address_id'], ['data.addresses.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('id_uuid'),
    schema='identity'
    )
    op.create_index(op.f('ix_identity_persons_id'), 'persons', ['id'], unique=False, schema='identity')
    op.create_index(op.f('ix_identity_persons_identity_hash'), 'persons', ['identity_hash'], unique=True, schema='identity')
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('hashed_password', sa.String(), nullable=True),
    sa.Column('role', postgresql.ENUM('superadmin', 'admin', 'region_admin', 'country_admin', 'moderator', 'sales_agent', 'user', 'service_owner', 'fleet_manager', 'driver', name='userrole', schema='identity'), nullable=False),
    sa.Column('person_id', sa.BigInteger(), nullable=True),
    sa.Column('subscription_plan', sa.String(length=30), server_default=sa.text("'FREE'"), nullable=False),
    sa.Column('subscription_expires_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_vip', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('referral_code', sa.String(length=20), nullable=True),
    sa.Column('referred_by_id', sa.Integer(), nullable=True),
    sa.Column('current_sales_agent_id', sa.Integer(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('folder_slug', sa.String(length=12), nullable=True),
    sa.Column('preferred_language', sa.String(length=5), server_default='hu', nullable=False),
    sa.Column('region_code', sa.String(length=5), server_default='HU', nullable=False),
    sa.Column('preferred_currency', sa.String(length=3), server_default='HUF', nullable=False),
    sa.Column('scope_level', sa.String(length=30), server_default='individual', nullable=False),
    sa.Column('scope_id', sa.String(length=50), nullable=True),
    sa.Column('custom_permissions', sa.JSON(), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['current_sales_agent_id'], ['identity.users.id'], ),
    sa.ForeignKeyConstraint(['person_id'], ['identity.persons.id'], ),
    sa.ForeignKeyConstraint(['referred_by_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('referral_code'),
    schema='identity'
    )
    op.create_index(op.f('ix_identity_users_email'), 'users', ['email'], unique=True, schema='identity')
    op.create_index(op.f('ix_identity_users_folder_slug'), 'users', ['folder_slug'], unique=True, schema='identity')
    op.create_index(op.f('ix_identity_users_id'), 'users', ['id'], unique=False, schema='identity')
    op.create_table('audit_logs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('severity', postgresql.ENUM('info', 'warning', 'critical', 'emergency', name='log_severity', schema='data'), nullable=False),
    sa.Column('action', sa.String(length=100), nullable=False),
    sa.Column('target_type', sa.String(length=50), nullable=True),
    sa.Column('target_id', sa.String(length=50), nullable=True),
    sa.Column('old_data', sa.JSON(), nullable=True),
    sa.Column('new_data', sa.JSON(), nullable=True),
    sa.Column('ip_address', sa.String(length=45), nullable=True),
    sa.Column('user_agent', sa.Text(), nullable=True),
    sa.Column('timestamp', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_audit_logs_action'), 'audit_logs', ['action'], unique=False, schema='data')
    op.create_index(op.f('ix_data_audit_logs_id'), 'audit_logs', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_audit_logs_ip_address'), 'audit_logs', ['ip_address'], unique=False, schema='data')
    op.create_index(op.f('ix_data_audit_logs_target_id'), 'audit_logs', ['target_id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_audit_logs_target_type'), 'audit_logs', ['target_type'], unique=False, schema='data')
    op.create_index(op.f('ix_data_audit_logs_timestamp'), 'audit_logs', ['timestamp'], unique=False, schema='data')
    op.create_table('organizations',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('address_id', sa.UUID(), nullable=True),
    sa.Column('is_anonymized', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('anonymized_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('full_name', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('display_name', sa.String(length=50), nullable=True),
    sa.Column('folder_slug', sa.String(length=12), nullable=False),
    sa.Column('default_currency', sa.String(length=3), nullable=False),
    sa.Column('country_code', sa.String(length=2), nullable=False),
    sa.Column('language', sa.String(length=5), nullable=False),
    sa.Column('address_zip', sa.String(length=10), nullable=True),
    sa.Column('address_city', sa.String(length=100), nullable=True),
    sa.Column('address_street_name', sa.String(length=150), nullable=True),
    sa.Column('address_street_type', sa.String(length=50), nullable=True),
    sa.Column('address_house_number', sa.String(length=20), nullable=True),
    sa.Column('address_hrsz', sa.String(length=50), nullable=True),
    sa.Column('tax_number', sa.String(length=20), nullable=True),
    sa.Column('reg_number', sa.String(length=50), nullable=True),
    sa.Column('org_type', postgresql.ENUM('individual', 'service', 'service_provider', 'fleet_owner', 'club', 'business', name='orgtype', schema='data'), nullable=False),
    sa.Column('status', sa.String(length=30), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('subscription_plan', sa.String(length=30), server_default=sa.text("'FREE'"), nullable=False),
    sa.Column('base_asset_limit', sa.Integer(), server_default=sa.text('1'), nullable=False),
    sa.Column('purchased_extra_slots', sa.Integer(), server_default=sa.text('0'), nullable=False),
    sa.Column('notification_settings', sa.JSON(), server_default=sa.text('\'{"notify_owner": true, "alert_days_before": [30, 15, 7, 1]}\'::jsonb'), nullable=False),
    sa.Column('external_integration_config', sa.JSON(), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('owner_id', sa.Integer(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_verified', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_ownership_transferable', sa.Boolean(), server_default=sa.text('true'), nullable=False),
    sa.ForeignKeyConstraint(['address_id'], ['data.addresses.id'], ),
    sa.ForeignKeyConstraint(['owner_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_organizations_folder_slug'), 'organizations', ['folder_slug'], unique=True, 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_subscription_plan'), 'organizations', ['subscription_plan'], unique=False, schema='data')
    op.create_index(op.f('ix_data_organizations_tax_number'), 'organizations', ['tax_number'], unique=True, schema='data')
    op.create_table('points_ledger',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('points', sa.Integer(), nullable=False),
    sa.Column('penalty_change', sa.Integer(), server_default=sa.text('0'), nullable=False),
    sa.Column('reason', sa.String(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_points_ledger_id'), 'points_ledger', ['id'], unique=False, schema='data')
    op.create_table('user_badges',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('badge_id', sa.Integer(), nullable=False),
    sa.Column('earned_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['badge_id'], ['data.badges.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_user_badges_id'), 'user_badges', ['id'], unique=False, schema='data')
    op.create_table('user_stats',
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('total_xp', sa.Integer(), nullable=False),
    sa.Column('social_points', sa.Integer(), nullable=False),
    sa.Column('current_level', sa.Integer(), nullable=False),
    sa.Column('penalty_points', sa.Integer(), server_default=sa.text('0'), nullable=False),
    sa.Column('restriction_level', sa.Integer(), server_default=sa.text('0'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('user_id'),
    schema='data'
    )
    op.create_table('social_accounts',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('provider', sa.String(length=50), nullable=False),
    sa.Column('social_id', sa.String(length=255), nullable=False),
    sa.Column('email', sa.String(length=255), nullable=False),
    sa.Column('extra_data', sa.JSON(), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('provider', 'social_id', name='uix_social_provider_id'),
    schema='identity'
    )
    op.create_index(op.f('ix_identity_social_accounts_id'), 'social_accounts', ['id'], unique=False, schema='identity')
    op.create_index(op.f('ix_identity_social_accounts_social_id'), 'social_accounts', ['social_id'], unique=False, schema='identity')
    op.create_table('verification_tokens',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('token', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('token_type', sa.String(length=20), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('is_used', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('token'),
    schema='identity'
    )
    op.create_index(op.f('ix_identity_verification_tokens_id'), 'verification_tokens', ['id'], unique=False, schema='identity')
    op.create_table('wallets',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('earned_credits', sa.Numeric(precision=18, scale=4), server_default=sa.text('0'), nullable=False),
    sa.Column('purchased_credits', sa.Numeric(precision=18, scale=4), server_default=sa.text('0'), nullable=False),
    sa.Column('service_coins', sa.Numeric(precision=18, scale=4), server_default=sa.text('0'), nullable=False),
    sa.Column('currency', sa.String(length=3), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id'),
    schema='identity'
    )
    op.create_index(op.f('ix_identity_wallets_id'), 'wallets', ['id'], unique=False, schema='identity')
    op.create_table('assets',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('vin', sa.String(length=17), nullable=False),
    sa.Column('license_plate', sa.String(length=20), nullable=True),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('year_of_manufacture', sa.Integer(), nullable=True),
    sa.Column('current_organization_id', sa.Integer(), nullable=True),
    sa.Column('catalog_id', sa.Integer(), nullable=True),
    sa.Column('is_verified', sa.Boolean(), nullable=False),
    sa.Column('verification_method', sa.String(length=20), nullable=True),
    sa.Column('verification_notes', sa.Text(), nullable=True),
    sa.Column('catalog_match_score', sa.Numeric(precision=5, scale=2), nullable=True),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('registration_uuid', sa.UUID(), nullable=False),
    sa.Column('is_corporate', sa.Boolean(), server_default=sa.text('false'), nullable=False),
    sa.Column('owner_person_id', sa.BigInteger(), nullable=True),
    sa.Column('owner_org_id', sa.Integer(), nullable=True),
    sa.Column('operator_person_id', sa.BigInteger(), nullable=True),
    sa.Column('operator_org_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['catalog_id'], ['data.vehicle_catalog.id'], ),
    sa.ForeignKeyConstraint(['current_organization_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['operator_org_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['operator_person_id'], ['identity.persons.id'], ),
    sa.ForeignKeyConstraint(['owner_org_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['owner_person_id'], ['identity.persons.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_assets_license_plate'), 'assets', ['license_plate'], unique=False, schema='data')
    op.create_index(op.f('ix_data_assets_registration_uuid'), 'assets', ['registration_uuid'], unique=False, schema='data')
    op.create_index(op.f('ix_data_assets_vin'), 'assets', ['vin'], unique=True, schema='data')
    op.create_table('branches',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=False),
    sa.Column('address_id', sa.UUID(), nullable=True),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('is_main', sa.Boolean(), nullable=False),
    sa.Column('postal_code', sa.String(length=10), nullable=True),
    sa.Column('city', sa.String(length=100), nullable=True),
    sa.Column('street_name', sa.String(length=150), nullable=True),
    sa.Column('street_type', sa.String(length=50), nullable=True),
    sa.Column('house_number', sa.String(length=20), nullable=True),
    sa.Column('stairwell', sa.String(length=20), nullable=True),
    sa.Column('floor', sa.String(length=20), nullable=True),
    sa.Column('door', sa.String(length=20), nullable=True),
    sa.Column('hrsz', sa.String(length=50), nullable=True),
    sa.Column('opening_hours', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('branch_rating', sa.Float(), nullable=False),
    sa.Column('status', sa.String(length=30), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['address_id'], ['data.addresses.id'], ),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_branches_city'), 'branches', ['city'], unique=False, schema='data')
    op.create_index(op.f('ix_data_branches_postal_code'), 'branches', ['postal_code'], unique=False, schema='data')
    op.create_table('credit_logs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('org_id', sa.Integer(), nullable=False),
    sa.Column('amount', sa.Numeric(precision=10, scale=2), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['org_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('org_sales_assignments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=True),
    sa.Column('agent_user_id', sa.Integer(), nullable=True),
    sa.Column('assigned_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['agent_user_id'], ['identity.users.id'], ),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('org_subscriptions',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('org_id', sa.Integer(), nullable=False),
    sa.Column('tier_id', sa.Integer(), nullable=False),
    sa.Column('valid_from', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('valid_until', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['org_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['tier_id'], ['data.subscription_tiers.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('organization_financials',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=False),
    sa.Column('year', sa.Integer(), nullable=False),
    sa.Column('turnover', sa.Numeric(precision=18, scale=2), nullable=True),
    sa.Column('profit', sa.Numeric(precision=18, scale=2), nullable=True),
    sa.Column('employee_count', sa.Integer(), nullable=True),
    sa.Column('source', sa.String(length=50), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_organization_financials_id'), 'organization_financials', ['id'], unique=False, schema='data')
    op.create_table('organization_members',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('person_id', sa.BigInteger(), nullable=True),
    sa.Column('role', postgresql.ENUM('OWNER', 'ADMIN', 'FLEET_MANAGER', 'DRIVER', 'MECHANIC', 'RECEPTIONIST', name='orguserrole', schema='data'), nullable=False),
    sa.Column('permissions', sa.JSON(), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('is_permanent', sa.Boolean(), nullable=False),
    sa.Column('is_verified', sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['person_id'], ['identity.persons.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    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('service_profiles',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=True),
    sa.Column('parent_id', sa.Integer(), nullable=True),
    sa.Column('fingerprint', sa.String(length=255), nullable=False),
    sa.Column('location', geoalchemy2.types.Geometry(geometry_type='POINT', srid=4326, dimension=2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False),
    sa.Column('status', sa.String(length=20), server_default=sa.text("'ghost'"), nullable=False),
    sa.Column('last_audit_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('google_place_id', sa.String(length=100), nullable=True),
    sa.Column('rating', sa.Float(), nullable=True),
    sa.Column('user_ratings_total', sa.Integer(), nullable=True),
    sa.Column('vibe_analysis', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('social_links', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('specialization_tags', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('trust_score', sa.Integer(), nullable=False),
    sa.Column('is_verified', sa.Boolean(), nullable=False),
    sa.Column('verification_log', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('opening_hours', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('contact_phone', sa.String(), nullable=True),
    sa.Column('contact_email', sa.String(), nullable=True),
    sa.Column('website', sa.String(), nullable=True),
    sa.Column('bio', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['parent_id'], ['data.service_profiles.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('google_place_id'),
    sa.UniqueConstraint('organization_id'),
    schema='data'
    )
    op.create_index('idx_service_fingerprint', 'service_profiles', ['fingerprint'], unique=True, schema='data')
    # op.create_index('idx_service_profiles_location', 'service_profiles', ['location'], unique=False, schema='data', postgresql_using='gist')
    op.create_index(op.f('ix_data_service_profiles_fingerprint'), 'service_profiles', ['fingerprint'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_profiles_id'), 'service_profiles', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_profiles_location'), 'service_profiles', ['location'], unique=False, schema='data')
    op.create_index(op.f('ix_data_service_profiles_status'), 'service_profiles', ['status'], unique=False, schema='data')
    op.create_table('asset_assignments',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=False),
    sa.Column('branch_id', sa.UUID(), nullable=True),
    sa.Column('assigned_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('released_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('status', sa.String(length=30), nullable=False),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.ForeignKeyConstraint(['branch_id'], ['data.branches.id'], ),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('asset_costs',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('organization_id', sa.Integer(), nullable=False),
    sa.Column('driver_id', sa.Integer(), nullable=True),
    sa.Column('cost_type', sa.String(length=50), nullable=False),
    sa.Column('amount_local', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('currency_local', sa.String(length=3), nullable=False),
    sa.Column('amount_eur', sa.Numeric(precision=18, scale=2), nullable=True),
    sa.Column('net_amount_local', sa.Numeric(precision=18, scale=2), nullable=True),
    sa.Column('vat_rate', sa.Numeric(precision=5, scale=2), nullable=True),
    sa.Column('exchange_rate_used', sa.Numeric(precision=18, scale=6), nullable=True),
    sa.Column('date', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('mileage_at_cost', sa.Integer(), nullable=True),
    sa.Column('data', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('registration_uuid', sa.UUID(), nullable=True),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.ForeignKeyConstraint(['driver_id'], ['identity.users.id'], ),
    sa.ForeignKeyConstraint(['organization_id'], ['data.organizations.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_asset_costs_registration_uuid'), 'asset_costs', ['registration_uuid'], unique=False, schema='data')
    op.create_table('asset_events',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('event_type', sa.String(length=50), nullable=False),
    sa.Column('recorded_mileage', sa.Integer(), nullable=True),
    sa.Column('data', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('registration_uuid', sa.UUID(), nullable=True),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_asset_events_registration_uuid'), 'asset_events', ['registration_uuid'], unique=False, schema='data')
    op.create_table('asset_financials',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('acquisition_price', sa.Numeric(precision=18, scale=2), nullable=True),
    sa.Column('acquisition_date', sa.DateTime(), nullable=True),
    sa.Column('financing_type', sa.String(), nullable=True),
    sa.Column('residual_value_estimate', sa.Numeric(precision=18, scale=2), nullable=True),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('asset_id'),
    schema='data'
    )
    op.create_table('asset_reviews',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('overall_rating', sa.Integer(), nullable=True),
    sa.Column('criteria_scores', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('comment', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_table('asset_telemetry',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('asset_id', sa.UUID(), nullable=False),
    sa.Column('current_mileage', sa.Integer(), nullable=False),
    sa.Column('mileage_unit', sa.String(length=10), nullable=False),
    sa.Column('vqi_score', sa.Numeric(precision=5, scale=2), nullable=False),
    sa.Column('dbs_score', sa.Numeric(precision=5, scale=2), nullable=False),
    sa.ForeignKeyConstraint(['asset_id'], ['data.assets.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('asset_id'),
    schema='data'
    )
    op.create_table('ratings',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('author_id', sa.Integer(), nullable=False),
    sa.Column('target_organization_id', sa.Integer(), nullable=True),
    sa.Column('target_user_id', sa.Integer(), nullable=True),
    sa.Column('target_branch_id', sa.UUID(), nullable=True),
    sa.Column('score', sa.Numeric(precision=3, scale=2), nullable=False),
    sa.Column('comment', sa.Text(), nullable=True),
    sa.Column('images', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'[]'::jsonb"), nullable=False),
    sa.Column('is_verified', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['author_id'], ['identity.users.id'], ),
    sa.ForeignKeyConstraint(['target_branch_id'], ['data.branches.id'], ),
    sa.ForeignKeyConstraint(['target_organization_id'], ['data.organizations.id'], ),
    sa.ForeignKeyConstraint(['target_user_id'], ['identity.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index('idx_rating_branch', 'ratings', ['target_branch_id'], unique=False, schema='data')
    op.create_index('idx_rating_org', 'ratings', ['target_organization_id'], unique=False, schema='data')
    op.create_index('idx_rating_user', 'ratings', ['target_user_id'], unique=False, schema='data')
    op.create_table('service_expertises',
    sa.Column('service_id', sa.Integer(), nullable=False),
    sa.Column('expertise_id', sa.Integer(), nullable=False),
    sa.Column('validation_level', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['expertise_id'], ['data.expertise_tags.id'], ),
    sa.ForeignKeyConstraint(['service_id'], ['data.service_profiles.id'], ),
    sa.PrimaryKeyConstraint('service_id', 'expertise_id'),
    schema='data'
    )
    op.create_table('vehicle_ownerships',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('vehicle_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('start_date', sa.Date(), server_default=sa.text('CURRENT_DATE'), nullable=False),
    sa.Column('end_date', sa.Date(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['identity.users.id'], ),
    sa.ForeignKeyConstraint(['vehicle_id'], ['data.assets.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicle_ownerships_id'), 'vehicle_ownerships', ['id'], unique=False, schema='data')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_data_vehicle_ownerships_id'), table_name='vehicle_ownerships', schema='data')
    op.drop_table('vehicle_ownerships', schema='data')
    op.drop_table('service_expertises', schema='data')
    op.drop_index('idx_rating_user', table_name='ratings', schema='data')
    op.drop_index('idx_rating_org', table_name='ratings', schema='data')
    op.drop_index('idx_rating_branch', table_name='ratings', schema='data')
    op.drop_table('ratings', schema='data')
    op.drop_table('asset_telemetry', schema='data')
    op.drop_table('asset_reviews', schema='data')
    op.drop_table('asset_financials', schema='data')
    op.drop_index(op.f('ix_data_asset_events_registration_uuid'), table_name='asset_events', schema='data')
    op.drop_table('asset_events', schema='data')
    op.drop_index(op.f('ix_data_asset_costs_registration_uuid'), table_name='asset_costs', schema='data')
    op.drop_table('asset_costs', schema='data')
    op.drop_table('asset_assignments', schema='data')
    op.drop_index(op.f('ix_data_service_profiles_status'), table_name='service_profiles', schema='data')
    op.drop_index(op.f('ix_data_service_profiles_location'), table_name='service_profiles', schema='data')
    op.drop_index(op.f('ix_data_service_profiles_id'), table_name='service_profiles', schema='data')
    op.drop_index(op.f('ix_data_service_profiles_fingerprint'), table_name='service_profiles', schema='data')
    op.drop_index('idx_service_profiles_location', table_name='service_profiles', schema='data', postgresql_using='gist')
    op.drop_index('idx_service_fingerprint', table_name='service_profiles', schema='data')
    op.drop_table('service_profiles', 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_organization_financials_id'), table_name='organization_financials', schema='data')
    op.drop_table('organization_financials', schema='data')
    op.drop_table('org_subscriptions', schema='data')
    op.drop_table('org_sales_assignments', schema='data')
    op.drop_table('credit_logs', schema='data')
    op.drop_index(op.f('ix_data_branches_postal_code'), table_name='branches', schema='data')
    op.drop_index(op.f('ix_data_branches_city'), table_name='branches', schema='data')
    op.drop_table('branches', schema='data')
    op.drop_index(op.f('ix_data_assets_vin'), table_name='assets', schema='data')
    op.drop_index(op.f('ix_data_assets_registration_uuid'), table_name='assets', schema='data')
    op.drop_index(op.f('ix_data_assets_license_plate'), table_name='assets', schema='data')
    op.drop_table('assets', schema='data')
    op.drop_index(op.f('ix_identity_wallets_id'), table_name='wallets', schema='identity')
    op.drop_table('wallets', schema='identity')
    op.drop_index(op.f('ix_identity_verification_tokens_id'), table_name='verification_tokens', schema='identity')
    op.drop_table('verification_tokens', schema='identity')
    op.drop_index(op.f('ix_identity_social_accounts_social_id'), table_name='social_accounts', schema='identity')
    op.drop_index(op.f('ix_identity_social_accounts_id'), table_name='social_accounts', schema='identity')
    op.drop_table('social_accounts', schema='identity')
    op.drop_table('user_stats', schema='data')
    op.drop_index(op.f('ix_data_user_badges_id'), table_name='user_badges', schema='data')
    op.drop_table('user_badges', schema='data')
    op.drop_index(op.f('ix_data_points_ledger_id'), table_name='points_ledger', schema='data')
    op.drop_table('points_ledger', 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_subscription_plan'), table_name='organizations', schema='data')
    op.drop_index(op.f('ix_data_organizations_id'), table_name='organizations', schema='data')
    op.drop_index(op.f('ix_data_organizations_folder_slug'), table_name='organizations', schema='data')
    op.drop_table('organizations', schema='data')
    op.drop_index(op.f('ix_data_audit_logs_timestamp'), table_name='audit_logs', schema='data')
    op.drop_index(op.f('ix_data_audit_logs_target_type'), table_name='audit_logs', schema='data')
    op.drop_index(op.f('ix_data_audit_logs_target_id'), table_name='audit_logs', schema='data')
    op.drop_index(op.f('ix_data_audit_logs_ip_address'), table_name='audit_logs', schema='data')
    op.drop_index(op.f('ix_data_audit_logs_id'), table_name='audit_logs', schema='data')
    op.drop_index(op.f('ix_data_audit_logs_action'), table_name='audit_logs', schema='data')
    op.drop_table('audit_logs', schema='data')
    op.drop_index(op.f('ix_identity_users_id'), table_name='users', schema='identity')
    op.drop_index(op.f('ix_identity_users_folder_slug'), table_name='users', schema='identity')
    op.drop_index(op.f('ix_identity_users_email'), table_name='users', schema='identity')
    op.drop_table('users', schema='identity')
    op.drop_index(op.f('ix_identity_persons_identity_hash'), table_name='persons', schema='identity')
    op.drop_index(op.f('ix_identity_persons_id'), table_name='persons', schema='identity')
    op.drop_table('persons', schema='identity')
    op.drop_index(op.f('ix_data_vehicle_catalog_power_kw'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_model'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_make'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_id'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_generation'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_fuel_type'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_engine_variant'), table_name='vehicle_catalog', schema='data')
    op.drop_index(op.f('ix_data_vehicle_catalog_engine_capacity'), table_name='vehicle_catalog', schema='data')
    op.drop_table('vehicle_catalog', schema='data')
    op.drop_table('model_feature_maps', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_year_to'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_year_from'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_technical_code'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_status'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_marketing_name'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_vehicle_model_definitions_make'), table_name='vehicle_model_definitions', schema='data')
    op.drop_index('idx_vmd_lookup', table_name='vehicle_model_definitions', schema='data')
    op.drop_table('vehicle_model_definitions', schema='data')
    op.drop_index(op.f('ix_data_geo_streets_name'), table_name='geo_streets', schema='data')
    op.drop_table('geo_streets', schema='data')
    op.drop_index(op.f('ix_data_feature_definitions_code'), table_name='feature_definitions', schema='data')
    op.drop_index(op.f('ix_data_feature_definitions_category'), table_name='feature_definitions', schema='data')
    op.drop_table('feature_definitions', schema='data')
    op.drop_table('addresses', schema='data')
    op.drop_index(op.f('ix_data_vehicle_types_code'), table_name='vehicle_types', schema='data')
    op.drop_table('vehicle_types', schema='data')
    op.drop_index(op.f('ix_data_translations_lang'), table_name='translations', schema='data')
    op.drop_index(op.f('ix_data_translations_key'), table_name='translations', schema='data')
    op.drop_index(op.f('ix_data_translations_id'), table_name='translations', schema='data')
    op.drop_table('translations', schema='data')
    op.drop_index(op.f('ix_data_subscription_tiers_name'), table_name='subscription_tiers', schema='data')
    op.drop_table('subscription_tiers', schema='data')
    op.drop_index(op.f('ix_data_service_staging_status'), table_name='service_staging', schema='data')
    op.drop_index(op.f('ix_data_service_staging_postal_code'), table_name='service_staging', schema='data')
    op.drop_index(op.f('ix_data_service_staging_name'), table_name='service_staging', schema='data')
    op.drop_index(op.f('ix_data_service_staging_id'), table_name='service_staging', schema='data')
    op.drop_index(op.f('ix_data_service_staging_city'), table_name='service_staging', schema='data')
    op.drop_index('idx_staging_fingerprint', table_name='service_staging', schema='data')
    op.drop_table('service_staging', schema='data')
    op.drop_index(op.f('ix_data_service_specialties_slug'), table_name='service_specialties', schema='data')
    op.drop_table('service_specialties', schema='data')
    op.drop_index(op.f('ix_data_point_rules_id'), table_name='point_rules', schema='data')
    op.drop_index(op.f('ix_data_point_rules_action_key'), table_name='point_rules', schema='data')
    op.drop_table('point_rules', schema='data')
    op.drop_index(op.f('ix_data_level_configs_id'), table_name='level_configs', schema='data')
    op.drop_table('level_configs', schema='data')
    op.drop_table('geo_street_types', schema='data')
    op.drop_index(op.f('ix_data_geo_postal_codes_zip_code'), table_name='geo_postal_codes', schema='data')
    op.drop_index(op.f('ix_data_geo_postal_codes_city'), table_name='geo_postal_codes', schema='data')
    op.drop_table('geo_postal_codes', schema='data')
    op.drop_index(op.f('ix_data_expertise_tags_key'), table_name='expertise_tags', schema='data')
    op.drop_table('expertise_tags', schema='data')
    op.drop_table('exchange_rates', schema='data')
    op.drop_table('discovery_parameters', schema='data')
    op.drop_index(op.f('ix_data_catalog_discovery_vehicle_class'), table_name='catalog_discovery', schema='data')
    op.drop_index(op.f('ix_data_catalog_discovery_status'), table_name='catalog_discovery', schema='data')
    op.drop_index(op.f('ix_data_catalog_discovery_model'), table_name='catalog_discovery', schema='data')
    op.drop_index(op.f('ix_data_catalog_discovery_make'), table_name='catalog_discovery', schema='data')
    op.drop_index(op.f('ix_data_catalog_discovery_id'), table_name='catalog_discovery', schema='data')
    op.drop_table('catalog_discovery', schema='data')
    op.drop_index(op.f('ix_data_badges_id'), table_name='badges', schema='data')
    op.drop_table('badges', schema='data')
    # ### end Alembic commands ###
