import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.createTable('dealer_completion_expenses', {
    expense_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: 'workflow_requests', key: 'request_id' },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
    completion_id: {
      type: DataTypes.UUID,
      allowNull: true,
      references: { model: 'dealer_completion_details', key: 'completion_id' },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: false,
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });

  await queryInterface.addIndex('dealer_completion_expenses', ['request_id'], {
    name: 'idx_dealer_completion_expenses_request_id',
  });
  await queryInterface.addIndex('dealer_completion_expenses', ['completion_id'], {
    name: 'idx_dealer_completion_expenses_completion_id',
  });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('dealer_completion_expenses');
}

