'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // For MySQL/MariaDB, we need to alter the ENUM type
    // First, check if we're using MySQL
    const [results] = await queryInterface.sequelize.query("SELECT VERSION() as version");
    const dbType = results[0]?.version ? 'mysql' : 'other';
    
    if (dbType === 'mysql') {
      // MySQL: Alter the column to include IN_TRANSIT
      await queryInterface.sequelize.query(`
        ALTER TABLE material_return_note 
        MODIFY COLUMN status ENUM('DRAFT', 'IN_TRANSIT', 'POSTED', 'CANCELLED') NOT NULL DEFAULT 'DRAFT'
      `);
    } else {
      // For other databases (PostgreSQL, SQLite), use Sequelize's changeColumn
      await queryInterface.changeColumn('material_return_note', 'status', {
        type: Sequelize.ENUM('DRAFT', 'IN_TRANSIT', 'POSTED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      });
    }
  },

  down: async (queryInterface, Sequelize) => {
    // Revert to original enum values
    const [results] = await queryInterface.sequelize.query("SELECT VERSION() as version");
    const dbType = results[0]?.version ? 'mysql' : 'other';
    
    if (dbType === 'mysql') {
      await queryInterface.sequelize.query(`
        ALTER TABLE material_return_note 
        MODIFY COLUMN status ENUM('DRAFT', 'POSTED', 'CANCELLED') NOT NULL DEFAULT 'DRAFT'
      `);
    } else {
      await queryInterface.changeColumn('material_return_note', 'status', {
        type: Sequelize.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'DRAFT'
      });
    }
  }
};

