"""upgrade_schema

Revision ID: c28fd63c44e0
Revises: 29f29759316a
Create Date: 2026-07-20 14:49:12.828392

"""
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 = 'c28fd63c44e0'
down_revision: Union[str, None] = '29f29759316a'
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! ###
    # Use IF EXISTS so this migration is safe on DBs that never had the old chassis tables
    op.execute('DROP TABLE IF EXISTS chassis_dalite_inspection CASCADE')
    op.execute('DROP TABLE IF EXISTS chassis_hold CASCADE')
    op.execute('DROP TABLE IF EXISTS chassis_fuel_log CASCADE')
    op.execute('DROP TABLE IF EXISTS chassis_parts_removed_log CASCADE')
    op.execute('DROP TABLE IF EXISTS chassis_receipt_acknowledgement CASCADE')
    op.execute('DROP TABLE IF EXISTS chassis_defect_head_master CASCADE')
    op.execute('DROP INDEX IF EXISTS ix_chassis_inward_chassis_number')
    op.execute('DROP INDEX IF EXISTS ix_chassis_inward_current_status')
    op.execute('DROP TABLE IF EXISTS chassis_inward CASCADE')
    op.execute('DROP TABLE IF EXISTS chassis_fuel_norm_master CASCADE')
    op.execute('ALTER TABLE IF EXISTS chassis_qa_checklist_result DROP CONSTRAINT IF EXISTS uq_chassis_qa_item')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_unique_constraint(op.f('uq_chassis_qa_item'), 'chassis_qa_checklist_result', ['chassis_id', 'item_id'], postgresql_nulls_not_distinct=False)
    op.create_table('chassis_receipt_acknowledgement',
    sa.Column('ack_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('chassis_inward_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('engine_motor_no', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('battery_make', sa.VARCHAR(length=100), autoincrement=False, nullable=True),
    sa.Column('battery_sl_no', sa.VARCHAR(length=100), autoincrement=False, nullable=True),
    sa.Column('color', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('configuration', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('depot_dealer', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('delivery_no', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('invoice_ge_no', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('invoice_date', sa.DATE(), autoincrement=False, nullable=True),
    sa.Column('km_reading_at_plant', sa.NUMERIC(precision=10, scale=1), autoincrement=False, nullable=True),
    sa.Column('km_reading_on_receipt', sa.NUMERIC(precision=10, scale=1), autoincrement=False, nullable=True),
    sa.Column('arrival_date', sa.DATE(), autoincrement=False, nullable=True),
    sa.Column('driver_name', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('items_supplied_json', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True),
    sa.Column('tyre_detail_json', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True),
    sa.Column('receipt_remarks', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['chassis_inward_id'], ['chassis_inward.chassis_inward_id'], name=op.f('chassis_receipt_acknowledgement_chassis_inward_id_fkey')),
    sa.PrimaryKeyConstraint('ack_id', name=op.f('chassis_receipt_acknowledgement_pkey'))
    )
    op.create_table('chassis_fuel_norm_master',
    sa.Column('norm_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('ac_flag', sa.BOOLEAN(), autoincrement=False, nullable=False),
    sa.Column('max_qty_ltr', sa.NUMERIC(precision=6, scale=2), autoincrement=False, nullable=False),
    sa.Column('is_active', sa.BOOLEAN(), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('norm_id', name=op.f('chassis_fuel_norm_master_pkey'))
    )
    op.create_table('chassis_inward',
    sa.Column('chassis_inward_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('chassis_number', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('model', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('fuel_type', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
    sa.Column('ac_flag', sa.BOOLEAN(), autoincrement=False, nullable=True),
    sa.Column('customer_id', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('transport_id', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('receiving_location_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('receiving_date', sa.DATE(), autoincrement=False, nullable=False),
    sa.Column('da_lite_status', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
    sa.Column('voltage', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
    sa.Column('remark', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('current_status', sa.VARCHAR(length=30), autoincrement=False, nullable=False),
    sa.Column('build_order_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('received_by', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('is_active', sa.BOOLEAN(), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.Column('updated_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['build_order_id'], ['build_order.order_id'], name='chassis_inward_build_order_id_fkey'),
    sa.ForeignKeyConstraint(['customer_id'], ['customer_master.customer_id'], name='chassis_inward_customer_id_fkey'),
    sa.ForeignKeyConstraint(['received_by'], ['login_users.user_id'], name='chassis_inward_received_by_fkey'),
    sa.ForeignKeyConstraint(['receiving_location_id'], ['station_master.station_id'], name='chassis_inward_receiving_location_id_fkey'),
    sa.ForeignKeyConstraint(['transport_id'], ['transport_master.transport_id'], name='chassis_inward_transport_id_fkey'),
    sa.PrimaryKeyConstraint('chassis_inward_id', name='chassis_inward_pkey'),
    postgresql_ignore_search_path=False
    )
    op.create_index(op.f('ix_chassis_inward_current_status'), 'chassis_inward', ['current_status'], unique=False)
    op.create_index(op.f('ix_chassis_inward_chassis_number'), 'chassis_inward', ['chassis_number'], unique=True)
    op.create_table('chassis_parts_removed_log',
    sa.Column('removed_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('chassis_inward_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('part_description', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('responsible_person', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('signature_name', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('removed_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['chassis_inward_id'], ['chassis_inward.chassis_inward_id'], name=op.f('chassis_parts_removed_log_chassis_inward_id_fkey')),
    sa.ForeignKeyConstraint(['responsible_person'], ['login_users.user_id'], name=op.f('chassis_parts_removed_log_responsible_person_fkey')),
    sa.PrimaryKeyConstraint('removed_id', name=op.f('chassis_parts_removed_log_pkey'))
    )
    op.create_table('chassis_hold',
    sa.Column('hold_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('chassis_inward_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('inspection_date', sa.DATE(), autoincrement=False, nullable=False),
    sa.Column('inspector_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('location', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('visual_observation', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('defect_head_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('plant', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
    sa.Column('responsibility_person_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('responsibility_party', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('severity', sa.VARCHAR(length=20), autoincrement=False, nullable=False),
    sa.Column('status', sa.VARCHAR(length=20), autoincrement=False, nullable=False),
    sa.Column('remarks', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('cleared_by', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('cleared_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['chassis_inward_id'], ['chassis_inward.chassis_inward_id'], name=op.f('chassis_hold_chassis_inward_id_fkey')),
    sa.ForeignKeyConstraint(['cleared_by'], ['login_users.user_id'], name=op.f('chassis_hold_cleared_by_fkey')),
    sa.ForeignKeyConstraint(['defect_head_id'], ['chassis_defect_head_master.defect_head_id'], name=op.f('chassis_hold_defect_head_id_fkey')),
    sa.ForeignKeyConstraint(['inspector_id'], ['login_users.user_id'], name=op.f('chassis_hold_inspector_id_fkey')),
    sa.ForeignKeyConstraint(['responsibility_person_id'], ['login_users.user_id'], name=op.f('chassis_hold_responsibility_person_id_fkey')),
    sa.PrimaryKeyConstraint('hold_id', name=op.f('chassis_hold_pkey'))
    )
    op.create_table('chassis_fuel_log',
    sa.Column('fuel_log_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('chassis_inward_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('station_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('responsible_person', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('log_date', sa.DATE(), autoincrement=False, nullable=False),
    sa.Column('in_time', postgresql.TIME(), autoincrement=False, nullable=True),
    sa.Column('out_time', postgresql.TIME(), autoincrement=False, nullable=True),
    sa.Column('fuel_qty_ltr', sa.NUMERIC(precision=6, scale=2), autoincrement=False, nullable=True),
    sa.Column('available_fuel_qty', sa.NUMERIC(precision=6, scale=2), autoincrement=False, nullable=True),
    sa.Column('filling_qty', sa.NUMERIC(precision=6, scale=2), autoincrement=False, nullable=True),
    sa.Column('total_fuel_qty', sa.NUMERIC(precision=6, scale=2), autoincrement=False, nullable=True),
    sa.Column('norm_exceeded', sa.BOOLEAN(), autoincrement=False, nullable=False),
    sa.Column('receiver_signature_name', sa.VARCHAR(length=150), autoincrement=False, nullable=True),
    sa.Column('signed_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['chassis_inward_id'], ['chassis_inward.chassis_inward_id'], name=op.f('chassis_fuel_log_chassis_inward_id_fkey')),
    sa.ForeignKeyConstraint(['responsible_person'], ['login_users.user_id'], name=op.f('chassis_fuel_log_responsible_person_fkey')),
    sa.ForeignKeyConstraint(['station_id'], ['station_master.station_id'], name=op.f('chassis_fuel_log_station_id_fkey')),
    sa.PrimaryKeyConstraint('fuel_log_id', name=op.f('chassis_fuel_log_pkey'))
    )
    op.create_table('chassis_dalite_inspection',
    sa.Column('dalite_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('chassis_inward_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('inspector_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('inspection_date', sa.DATE(), autoincrement=False, nullable=False),
    sa.Column('vehicle_info_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('error_reading_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('operational_test_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('regen_precondition_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('idle_rpm_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('parameter_check_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('battery_voltage_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('error_verification_result', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('overall_result', sa.VARCHAR(length=10), autoincrement=False, nullable=False),
    sa.Column('defect_description', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('defect_head_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['chassis_inward_id'], ['chassis_inward.chassis_inward_id'], name=op.f('chassis_dalite_inspection_chassis_inward_id_fkey')),
    sa.ForeignKeyConstraint(['defect_head_id'], ['chassis_defect_head_master.defect_head_id'], name=op.f('chassis_dalite_inspection_defect_head_id_fkey')),
    sa.ForeignKeyConstraint(['inspector_id'], ['login_users.user_id'], name=op.f('chassis_dalite_inspection_inspector_id_fkey')),
    sa.PrimaryKeyConstraint('dalite_id', name=op.f('chassis_dalite_inspection_pkey'))
    )
    op.create_table('chassis_defect_head_master',
    sa.Column('defect_head_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('head_code', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('head_name', sa.VARCHAR(length=150), autoincrement=False, nullable=False),
    sa.Column('is_active', sa.BOOLEAN(), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('defect_head_id', name=op.f('chassis_defect_head_master_pkey')),
    sa.UniqueConstraint('head_code', name=op.f('chassis_defect_head_master_head_code_key'), postgresql_include=[], postgresql_nulls_not_distinct=False)
    )
    # ### end Alembic commands ###
