"""rebuild_schema_v2

Revision ID: 553ef1388276
Revises: c21c2c7e70d4
Create Date: 2026-01-25 11:32:10.692756

"""
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 = '553ef1388276'
down_revision: Union[str, Sequence[str], None] = 'c21c2c7e70d4'
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('system_settings',
    sa.Column('key', sa.String(), nullable=False),
    sa.Column('value', sa.JSON(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('key'),
    schema='data'
    )
    op.create_index(op.f('ix_data_system_settings_key'), 'system_settings', ['key'], unique=False, schema='data')
    op.drop_table('costs', schema='data')
    op.drop_table('point_rules', schema='data')
    op.drop_table('regional_settings', schema='data')
    op.drop_table('level_configs', schema='data')
    op.drop_table('vehicle_history', schema='data')
    op.alter_column('badges', 'description',
                existing_type=sa.TEXT(),
                type_=sa.String(length=255),
                nullable=False,
                schema='data')
    op.alter_column('badges', 'icon_url',
                existing_type=sa.VARCHAR(length=255),
                type_=sa.String(length=500),
                existing_nullable=True,
                schema='data')
    op.create_index(op.f('ix_data_badges_id'), 'badges', ['id'], unique=False, schema='data')
    op.create_unique_constraint(None, 'badges', ['name'], schema='data')
    op.drop_column('badges', 'created_at', schema='data')
    op.drop_column('badges', 'criteria_json', schema='data')
    op.alter_column('points_ledger', 'user_id',
                existing_type=sa.INTEGER(),
                nullable=False,
                schema='data')
    op.alter_column('points_ledger', 'timestamp',
                existing_type=postgresql.TIMESTAMP(timezone=True),
                nullable=False,
                existing_server_default=sa.text('now()'),
                schema='data')
    op.create_index(op.f('ix_data_points_ledger_id'), 'points_ledger', ['id'], unique=False, schema='data')
    op.create_index(op.f('ix_data_points_ledger_user_id'), 'points_ledger', ['user_id'], unique=False, schema='data')
    op.add_column('translations', sa.Column('is_published', sa.Boolean(), nullable=True), schema='data')
    op.drop_constraint('translations_key_lang_code_key', 'translations', schema='data', type_='unique')
    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_code'), 'translations', ['lang_code'], unique=False, schema='data')
    op.create_unique_constraint('uq_translation_key_lang', 'translations', ['key', 'lang_code'], schema='data')
    op.alter_column('user_badges', 'user_id',
                existing_type=sa.INTEGER(),
                nullable=False,
                schema='data')
    op.alter_column('user_badges', 'badge_id',
                existing_type=sa.INTEGER(),
                nullable=False,
                schema='data')
    op.alter_column('user_badges', 'earned_at',
                existing_type=postgresql.TIMESTAMP(timezone=True),
                nullable=False,
                existing_server_default=sa.text('now()'),
                schema='data')
    op.drop_constraint('user_badges_user_id_badge_id_key', 'user_badges', schema='data', type_='unique')
    op.alter_column('user_stats', 'total_points',
                existing_type=sa.INTEGER(),
                nullable=False,
                existing_server_default=sa.text('0'),
                schema='data')
    op.alter_column('user_stats', 'current_level',
                existing_type=sa.INTEGER(),
                nullable=False,
                existing_server_default=sa.text('1'),
                schema='data')
    op.alter_column('user_stats', 'last_updated',
                existing_type=postgresql.TIMESTAMP(timezone=True),
                nullable=False,
                existing_server_default=sa.text('now()'),
                schema='data')

    # --- JAVÍTOTT RÉSZ KEZDETE (Role konverzió default kezeléssel) ---
    # 1. Meglévő default eldobása
    op.execute('ALTER TABLE data.users ALTER COLUMN role DROP DEFAULT')
    
    # 2. Típusmódosítás
    op.alter_column('users', 'role',
                existing_type=postgresql.ENUM('SUPERUSER', 'REGIONAL_ADMIN', 'MODERATOR', 'BUSINESS_PARTNER', 'USER', name='user_role', schema='data'),
                type_=sa.Enum('SUPERUSER', 'REGIONAL_ADMIN', 'MODERATOR', 'BUSINESS_PARTNER', 'USER', name='userrole'),
                nullable=False,
                schema='data',
                postgresql_using='role::text::userrole')
    
    # 3. Új default beállítása az új típussal
    op.execute("ALTER TABLE data.users ALTER COLUMN role SET DEFAULT 'USER'::userrole")
    # --- JAVÍTOTT RÉSZ VÉGE ---

    op.drop_index('idx_users_role', table_name='users', schema='data')
    op.create_index(op.f('ix_data_users_region_code'), 'users', ['region_code'], unique=False, schema='data')
    op.create_foreign_key(None, 'vehicle_assignments', 'user_vehicles', ['vehicle_id'], ['id'], source_schema='data', referent_schema='data')
    op.drop_index('ix_data_vehicle_events_vehicle_id', table_name='vehicle_events', schema='data')
    op.create_foreign_key(None, 'vehicle_events', 'user_vehicles', ['vehicle_id'], ['id'], source_schema='data', referent_schema='data')
    op.create_foreign_key(None, 'vehicle_ownerships', 'user_vehicles', ['vehicle_id'], ['id'], source_schema='data', referent_schema='data')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'vehicle_ownerships', schema='data', type_='foreignkey')
    op.drop_constraint(None, 'vehicle_events', schema='data', type_='foreignkey')
    op.create_index('ix_data_vehicle_events_vehicle_id', 'vehicle_events', ['vehicle_id'], unique=False, schema='data')
    op.drop_constraint(None, 'vehicle_assignments', schema='data', type_='foreignkey')
    op.drop_index(op.f('ix_data_users_region_code'), table_name='users', schema='data')
    op.create_index('idx_users_role', 'users', ['role'], unique=False, schema='data')
    op.alter_column('users', 'role',
               existing_type=sa.Enum('SUPERUSER', 'REGIONAL_ADMIN', 'MODERATOR', 'BUSINESS_PARTNER', 'USER', name='userrole'),
               type_=postgresql.ENUM('SUPERUSER', 'REGIONAL_ADMIN', 'MODERATOR', 'BUSINESS_PARTNER', 'USER', name='user_role', schema='data'),
               nullable=True,
               existing_server_default=sa.text("'USER'::data.user_role"),
               schema='data')
    op.alter_column('user_stats', 'last_updated',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'),
               schema='data')
    op.alter_column('user_stats', 'current_level',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('1'),
               schema='data')
    op.alter_column('user_stats', 'total_points',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'),
               schema='data')
    op.create_unique_constraint('user_badges_user_id_badge_id_key', 'user_badges', ['user_id', 'badge_id'], schema='data')
    op.alter_column('user_badges', 'earned_at',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'),
               schema='data')
    op.alter_column('user_badges', 'badge_id',
               existing_type=sa.INTEGER(),
               nullable=True,
               schema='data')
    op.alter_column('user_badges', 'user_id',
               existing_type=sa.INTEGER(),
               nullable=True,
               schema='data')
    op.drop_constraint('uq_translation_key_lang', 'translations', schema='data', type_='unique')
    op.drop_index(op.f('ix_data_translations_lang_code'), 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.create_unique_constraint('translations_key_lang_code_key', 'translations', ['key', 'lang_code'], schema='data')
    op.drop_column('translations', 'is_published', schema='data')
    op.drop_index(op.f('ix_data_points_ledger_user_id'), table_name='points_ledger', schema='data')
    op.drop_index(op.f('ix_data_points_ledger_id'), table_name='points_ledger', schema='data')
    op.alter_column('points_ledger', 'timestamp',
               existing_type=postgresql.TIMESTAMP(timezone=True),
               nullable=True,
               existing_server_default=sa.text('now()'),
               schema='data')
    op.alter_column('points_ledger', 'user_id',
               existing_type=sa.INTEGER(),
               nullable=True,
               schema='data')
    op.add_column('badges', sa.Column('criteria_json', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True), schema='data')
    op.add_column('badges', sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=True), schema='data')
    op.drop_constraint(None, 'badges', schema='data', type_='unique')
    op.drop_index(op.f('ix_data_badges_id'), table_name='badges', schema='data')
    op.alter_column('badges', 'icon_url',
               existing_type=sa.String(length=500),
               type_=sa.VARCHAR(length=255),
               existing_nullable=True,
               schema='data')
    op.alter_column('badges', 'description',
               existing_type=sa.String(length=255),
               type_=sa.TEXT(),
               nullable=True,
               schema='data')
    op.create_table('vehicle_history',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('data.vehicle_history_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('vehicle_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('user_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('role', sa.VARCHAR(length=20), autoincrement=False, nullable=False),
    sa.Column('start_date', sa.DATE(), autoincrement=False, nullable=False),
    sa.Column('end_date', sa.DATE(), autoincrement=False, nullable=True),
    sa.Column('start_mileage', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('end_mileage', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), sa.Computed('(end_date IS NULL)', persisted=True), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], name='vehicle_history_user_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='vehicle_history_pkey'),
    schema='data'
    )
    op.create_table('level_configs',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('data.level_configs_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('level_number', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('min_points', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('name_translation_key', sa.VARCHAR(length=100), autoincrement=False, nullable=True),
    sa.Column('region_code', sa.VARCHAR(length=5), server_default=sa.text("'GLOBAL'::character varying"), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id', name='level_configs_pkey'),
    sa.UniqueConstraint('level_number', name='level_configs_level_number_key'),
    schema='data'
    )
    op.create_table('regional_settings',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('data.regional_settings_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('region_code', sa.VARCHAR(length=5), autoincrement=False, nullable=False),
    sa.Column('setting_key', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('value', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=False),
    sa.Column('start_date', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=True),
    sa.Column('end_date', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id', name='regional_settings_pkey'),
    schema='data'
    )
    op.create_table('point_rules',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('data.point_rules_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('rule_key', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('points', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.Column('start_date', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=True),
    sa.Column('end_date', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.Column('region_code', sa.VARCHAR(length=5), server_default=sa.text("'GLOBAL'::character varying"), autoincrement=False, nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), server_default=sa.text('true'), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id', name='point_rules_pkey'),
    schema='data'
    )
    op.create_table('alembic_version',
    sa.Column('version_num', sa.VARCHAR(length=32), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('version_num', name='alembic_version_pkc')
    )
    op.create_table('costs',
    sa.Column('id', sa.INTEGER(), server_default=sa.text("nextval('data.costs_id_seq'::regclass)"), autoincrement=True, nullable=False),
    sa.Column('vehicle_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('user_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('cost_type', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('amount', sa.NUMERIC(precision=10, scale=2), autoincrement=False, nullable=False),
    sa.Column('date', sa.DATE(), autoincrement=False, nullable=False),
    sa.Column('mileage_at_cost', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('description', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('document_url', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(), server_default=sa.text('now()'), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], name='costs_user_id_fkey'),
    sa.PrimaryKeyConstraint('id', name='costs_pkey'),
    schema='data'
    )
    op.drop_index(op.f('ix_data_system_settings_key'), table_name='system_settings', schema='data')
    op.drop_table('system_settings', schema='data')
    # ### end Alembic commands ###
