'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('scrap_reason_master', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      reason_code: {
        type: Sequelize.STRING(20),
        allowNull: false,
        comment: 'Unique code for scrap reason (e.g., DMG, EXP, OBS)'
      },
      reason_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: 'Name of scrap reason (e.g., Damaged, Expired, Obsolete)'
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('scrap_reason_master', ['company_id']);
    await queryInterface.addIndex('scrap_reason_master', ['reason_code']);
    await queryInterface.addIndex('scrap_reason_master', ['is_active']);
    await queryInterface.addIndex('scrap_reason_master', ['status']);
    await queryInterface.addIndex('scrap_reason_master', ['company_id', 'reason_code'], {
      unique: true,
      name: 'unique_scrap_reason_code'
    });
  },

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

