'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('vendor_quotation_request_item', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      vqr_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'vendor_quotation_request',
          key: 'id'
        },
        onDelete: 'CASCADE'
      },
      rfq_item_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'request_for_quotation_item',
          key: 'id'
        }
      },
      line_number: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        }
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        }
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      // Vendor response fields
      quoted_price: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: true
      },
      quoted_delivery_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      vendor_remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('vendor_quotation_request_item', ['vqr_id']);
    await queryInterface.addIndex('vendor_quotation_request_item', ['material_id']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('vendor_quotation_request_item');
  }
};

