'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('delivery_challan_item', {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
      delivery_challan_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'delivery_challan', key: 'id', onDelete: 'CASCADE' }
      },
      item_number: { type: Sequelize.INTEGER, allowNull: false },
      invoice_item_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'sales_invoice_item', key: 'id' }
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' }
      },
      item_code: { type: Sequelize.STRING(60), allowNull: true },
      item_name: { type: Sequelize.STRING(300), allowNull: true },
      hsn_code: { type: Sequelize.STRING(20), allowNull: true },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'uom', key: 'id' }
      },
      order_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      invoice_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      dispatch_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      delivered_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('delivery_challan_item', ['delivery_challan_id']);
    await queryInterface.addIndex('delivery_challan_item', ['material_id']);
    await queryInterface.addIndex('delivery_challan_item', ['invoice_item_id']);
  },

  async down(queryInterface) {
    await queryInterface.dropTable('delivery_challan_item');
  }
};

