'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('batch_master', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      batch_number: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Unique batch number for this material'
      },
      lot_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Lot number (may be same as batch or different)'
      },
      manufacturing_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Date when batch was manufactured'
      },
      expiry_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Expiry date for the batch'
      },
      mfg_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Manufacturing date (alternative field)'
      },
      vendor_batch_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Vendor batch number if different from internal batch'
      },
      initial_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0.000,
        comment: 'Initial quantity when batch was created'
      },
      current_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0.000,
        comment: 'Current available quantity for this batch'
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Primary storage location for this batch'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'EXPIRED', 'CONSUMED', 'QUARANTINED', 'BLOCKED'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      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')
      }
    });

    // Add indexes
    await queryInterface.addIndex('batch_master', ['company_id', 'plant_id'], {
      name: 'batch_master_company_plant_idx'
    });
    await queryInterface.addIndex('batch_master', ['material_id'], {
      name: 'batch_master_material_idx'
    });
    await queryInterface.addIndex('batch_master', ['batch_number'], {
      name: 'batch_master_batch_number_idx'
    });
    await queryInterface.addIndex('batch_master', ['status'], {
      name: 'batch_master_status_idx'
    });
    await queryInterface.addIndex('batch_master', ['expiry_date'], {
      name: 'batch_master_expiry_date_idx'
    });
    await queryInterface.addIndex('batch_master', ['company_id', 'plant_id', 'material_id', 'batch_number'], {
      unique: true,
      name: 'batch_master_unique_idx'
    });
  },

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

