'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('gate_entry_item', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      gate_entry_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'gate_entry',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      po_item_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'purchase_order_item',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Reference to Purchase Order Item'
      },
      line_number: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      po_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Quantity as per Purchase Order'
      },
      received_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        comment: 'Quantity actually received at gate'
      },
      accepted_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Quantity accepted after QC check (if QC required)'
      },
      rejected_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        defaultValue: 0,
        comment: 'Quantity rejected by QC'
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      unit_price: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      batch_number: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      condition: {
        type: Sequelize.ENUM('GOOD', 'DAMAGED', 'PARTIAL_DAMAGE'),
        allowNull: true,
        defaultValue: 'GOOD',
        comment: 'Condition of material at gate entry'
      },
      qc_status: {
        type: Sequelize.ENUM('PENDING', 'APPROVED', 'REJECTED', 'NOT_REQUIRED'),
        allowNull: false,
        defaultValue: 'NOT_REQUIRED',
        comment: 'QC status: PENDING, APPROVED, REJECTED, NOT_REQUIRED'
      },
      qc_remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: 'QC remarks/observations'
      },
      qc_checked_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      qc_checked_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      grn_item_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'goods_receipt_note_item',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'GRN Item created from this gate entry item'
      },
      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('gate_entry_item', ['gate_entry_id'], {
      name: 'gate_entry_item_gate_entry_id_idx'
    });

    await queryInterface.addIndex('gate_entry_item', ['po_item_id'], {
      name: 'gate_entry_item_po_item_id_idx'
    });

    await queryInterface.addIndex('gate_entry_item', ['material_id'], {
      name: 'gate_entry_item_material_id_idx'
    });

    await queryInterface.addIndex('gate_entry_item', ['qc_status'], {
      name: 'gate_entry_item_qc_status_idx'
    });

    await queryInterface.addIndex('gate_entry_item', ['grn_item_id'], {
      name: 'gate_entry_item_grn_item_id_idx'
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('gate_entry_item');
  }
};

