"""Sync dealer orders 2

Revision ID: 071a1e0e9799
Revises: fcfbd9364b5f
Create Date: 2026-07-20 17:22:21.842128

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '071a1e0e9799'
down_revision: Union[str, None] = 'fcfbd9364b5f'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    conn = op.get_bind()
    from sqlalchemy.engine.reflection import Inspector
    inspector = Inspector.from_engine(conn)
    tables = inspector.get_table_names()

    if 'sales_enquiry' not in tables:
        op.create_table('sales_enquiry',
        sa.Column('enquiry_id', sa.UUID(), nullable=False),
        sa.Column('customer_id', sa.String(length=50), nullable=False),
        sa.Column('request_details', sa.Text(), nullable=False),
        sa.Column('status', sa.String(length=20), nullable=False),
        sa.Column('created_by', sa.UUID(), nullable=False),
        sa.Column('assigned_to', sa.UUID(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=True),
        sa.Column('updated_at', sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(['assigned_to'], ['login_users.user_id'], ),
        sa.ForeignKeyConstraint(['created_by'], ['login_users.user_id'], ),
        sa.ForeignKeyConstraint(['customer_id'], ['customer_master.customer_id'], ),
        sa.PrimaryKeyConstraint('enquiry_id')
        )
    if 'dealer_order' not in tables:
        op.create_table('dealer_order',
        sa.Column('order_id', sa.UUID(), nullable=False),
        sa.Column('order_no', sa.String(length=50), nullable=False),
        sa.Column('dealer_id', sa.String(length=50), nullable=False),
        sa.Column('client_id', sa.String(length=50), nullable=False),
        sa.Column('status', sa.String(length=30), nullable=False),
        sa.Column('remarks', sa.Text(), nullable=True),
        sa.Column('submitted_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('confirmed_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('build_order_id', sa.UUID(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(['build_order_id'], ['build_order.order_id'], ),
        sa.ForeignKeyConstraint(['client_id'], ['dealer_client_master.client_id'], ),
        sa.ForeignKeyConstraint(['dealer_id'], ['customer_master.customer_id'], ),
        sa.PrimaryKeyConstraint('order_id'),
        sa.UniqueConstraint('order_no')
        )


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'quote_header', type_='foreignkey')
    op.drop_constraint(None, 'dealer_portal_log', type_='foreignkey')
    op.drop_constraint(None, 'dealer_portal_admin_action_log', type_='foreignkey')
    op.drop_constraint(None, 'dealer_portal_action_log', type_='foreignkey')
    op.drop_constraint(None, 'dealer_order_vehicle', type_='foreignkey')
    op.drop_constraint(None, 'dealer_order_activity_log', type_='foreignkey')
    op.drop_constraint(None, 'dealer_order_activity_log', type_='foreignkey')
    op.drop_constraint(None, 'dealer_client_master', type_='foreignkey')
    op.alter_column('customer_master', 'portal_enabled',
               existing_type=sa.BOOLEAN(),
               nullable=True,
               existing_server_default=sa.text('false'))
    op.drop_constraint(None, 'chassis_master', type_='foreignkey')
    op.drop_constraint(None, 'chassis_master', type_='foreignkey')
    op.drop_constraint(None, 'chassis_master', type_='foreignkey')
    op.drop_constraint(None, 'chassis_master', type_='foreignkey')
    op.drop_table('dealer_order')
    op.drop_table('sales_enquiry')
    # ### end Alembic commands ###
