"""Clean gamification setup

Revision ID: c21c2c7e70d4
Revises: 
Create Date: 2026-01-24 11:19:10.464212

"""
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 = 'c21c2c7e70d4'
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.create_table('audit_logs',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('target_type', sa.String(), nullable=True),
    sa.Column('target_id', sa.Integer(), nullable=True),
    sa.Column('action', sa.String(), nullable=False),
    sa.Column('changes', sa.JSON(), nullable=True),
    sa.Column('ip_address', sa.String(), nullable=True),
    sa.Column('user_agent', sa.String(), nullable=True),
    sa.Column('timestamp', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    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_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_table('companies',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('tax_number', sa.String(), nullable=True),
    sa.Column('subscription_tier', sa.String(), nullable=True),
    sa.Column('owner_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['owner_id'], ['data.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_companies_id'), 'companies', ['id'], unique=False, schema='data')
    op.create_table('company_members',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('role', sa.Enum('OWNER', 'MANAGER', 'DRIVER', name='companyrole'), nullable=False),
    sa.Column('can_edit_service', sa.Boolean(), nullable=True),
    sa.Column('can_see_costs', sa.Boolean(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['data.companies.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_company_members_id'), 'company_members', ['id'], unique=False, schema='data')
    op.create_table('vehicle_assignments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('vehicle_id', sa.Integer(), nullable=False),
    sa.Column('driver_id', sa.Integer(), nullable=False),
    sa.Column('start_date', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
    sa.Column('end_date', sa.DateTime(timezone=True), nullable=True),
    sa.Column('start_odometer', sa.Integer(), nullable=True),
    sa.Column('end_odometer', sa.Integer(), nullable=True),
    sa.Column('notes', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['data.companies.id'], ),
    sa.ForeignKeyConstraint(['driver_id'], ['data.users.id'], ),
    sa.ForeignKeyConstraint(['vehicle_id'], ['data.vehicles.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicle_assignments_id'), 'vehicle_assignments', ['id'], unique=False, schema='data')
    op.create_table('vehicle_ownerships',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('vehicle_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('start_date', sa.Date(), nullable=False),
    sa.Column('end_date', sa.Date(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['data.users.id'], ),
    sa.ForeignKeyConstraint(['vehicle_id'], ['data.vehicles.id'], ),
    sa.PrimaryKeyConstraint('id'),
    schema='data'
    )
    op.create_index(op.f('ix_data_vehicle_ownerships_id'), 'vehicle_ownerships', ['id'], unique=False, schema='data')
  #  op.drop_table('costs', schema='data')
  #  op.drop_table('alembic_version')
  #  op.drop_table('vehicle_history', schema='data')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('vehicle_history',
    sa.Column('id', sa.INTEGER(), 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=op.f('vehicle_history_user_id_fkey')),
    sa.ForeignKeyConstraint(['vehicle_id'], ['data.vehicles.id'], name=op.f('vehicle_history_vehicle_id_fkey')),
    sa.PrimaryKeyConstraint('id', name=op.f('vehicle_history_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=op.f('alembic_version_pkc'))
    )
    op.create_table('costs',
    sa.Column('id', sa.INTEGER(), 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=op.f('costs_user_id_fkey')),
    sa.ForeignKeyConstraint(['vehicle_id'], ['data.vehicles.id'], name=op.f('costs_vehicle_id_fkey')),
    sa.PrimaryKeyConstraint('id', name=op.f('costs_pkey')),
    schema='data'
    )
    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_index(op.f('ix_data_vehicle_assignments_id'), table_name='vehicle_assignments', schema='data')
    op.drop_table('vehicle_assignments', schema='data')
    op.drop_index(op.f('ix_data_company_members_id'), table_name='company_members', schema='data')
    op.drop_table('company_members', schema='data')
    op.drop_index(op.f('ix_data_companies_id'), table_name='companies', schema='data')
    op.drop_table('companies', 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_id'), table_name='audit_logs', schema='data')
    op.drop_table('audit_logs', schema='data')
    # ### end Alembic commands ###
