'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('uom', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      uom_code: {
        type: Sequelize.STRING(10),
        allowNull: false,
        unique: true
      },
      uom_name: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      uom_description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      uom_type: {
        type: Sequelize.ENUM('WEIGHT', 'LENGTH', 'VOLUME', 'AREA', 'PIECE', 'TIME', 'ENERGY', 'OTHER'),
        allowNull: true
      },
      decimal_places: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 3
      },
      is_base_uom: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: 'True if this is a base unit of measure'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('uom', ['uom_code'], { unique: true });
    await queryInterface.addIndex('uom', ['status']);
    await queryInterface.addIndex('uom', ['uom_type']);
  },

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

