'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const rows = [
      { uom_code: 'PC', uom_name: 'Piece', uom_description: 'Unit/Piece', uom_type: 'PIECE', decimal_places: 0, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'KG', uom_name: 'Kilogram', uom_description: 'Kilogram', uom_type: 'WEIGHT', decimal_places: 3, is_base_uom: 1, status: 'ACTIVE' },
      { uom_code: 'MT', uom_name: 'Metric Ton', uom_description: 'Metric Ton (1000 KG)', uom_type: 'WEIGHT', decimal_places: 3, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'GM', uom_name: 'Gram', uom_description: 'Gram', uom_type: 'WEIGHT', decimal_places: 3, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'LTR', uom_name: 'Liter', uom_description: 'Liter', uom_type: 'VOLUME', decimal_places: 3, is_base_uom: 1, status: 'ACTIVE' },
      { uom_code: 'ML', uom_name: 'Milliliter', uom_description: 'Milliliter', uom_type: 'VOLUME', decimal_places: 3, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'MTR', uom_name: 'Meter', uom_description: 'Meter', uom_type: 'LENGTH', decimal_places: 3, is_base_uom: 1, status: 'ACTIVE' },
      { uom_code: 'CM', uom_name: 'Centimeter', uom_description: 'Centimeter', uom_type: 'LENGTH', decimal_places: 2, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'M2', uom_name: 'Square Meter', uom_description: 'Square Meter', uom_type: 'AREA', decimal_places: 2, is_base_uom: 1, status: 'ACTIVE' },
      { uom_code: 'M3', uom_name: 'Cubic Meter', uom_description: 'Cubic Meter', uom_type: 'VOLUME', decimal_places: 3, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'HR', uom_name: 'Hour', uom_description: 'Hour', uom_type: 'TIME', decimal_places: 2, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'MIN', uom_name: 'Minute', uom_description: 'Minute', uom_type: 'TIME', decimal_places: 0, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'BOX', uom_name: 'Box', uom_description: 'Box', uom_type: 'PIECE', decimal_places: 0, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'PKT', uom_name: 'Packet', uom_description: 'Packet', uom_type: 'PIECE', decimal_places: 0, is_base_uom: 0, status: 'ACTIVE' },
      { uom_code: 'CASE', uom_name: 'Case', uom_description: 'Case', uom_type: 'PIECE', decimal_places: 0, is_base_uom: 0, status: 'ACTIVE' }
    ];
    const dialect = queryInterface.sequelize.getDialect ? queryInterface.sequelize.getDialect() : 'mysql';
    if (dialect === 'mysql' || dialect === 'mariadb') {
      for (const r of rows) {
        await queryInterface.sequelize.query(
          `INSERT IGNORE INTO uom (uom_code, uom_name, uom_description, uom_type, decimal_places, is_base_uom, status, created_at, updated_at)
           VALUES (:uom_code, :uom_name, :uom_description, :uom_type, :decimal_places, :is_base_uom, :status, NOW(), NOW())`,
          { replacements: r }
        );
      }
    } else {
      await queryInterface.bulkInsert('uom', rows.map((r) => ({
        ...r,
        is_base_uom: !!r.is_base_uom,
        created_at: new Date(),
        updated_at: new Date()
      })), { ignoreDuplicates: true });
    }
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('uom', null, {});
  }
};

