'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('material_substitution_transaction', {
      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'
      },
      substitution_master_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'material_substitution_master',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Reference to substitution master rule (if applicable)'
      },
      reference_type: {
        type: Sequelize.ENUM('PRODUCTION_ORDER', 'MATERIAL_REQUEST', 'BOM', 'OTHER'),
        allowNull: false,
        comment: 'Type of document where substitution occurred'
      },
      reference_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'ID of the document (Production Order, Material Request, etc.)'
      },
      reference_item_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: 'ID of the item within the document (ProductionOrderComponent, MaterialRequestItem, etc.)'
      },
      original_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      substitute_material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      original_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        comment: 'Original quantity required'
      },
      substitute_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        comment: 'Substitute quantity used'
      },
      substitution_ratio: {
        type: Sequelize.DECIMAL(10, 4),
        allowNull: false,
        comment: 'Ratio used for this substitution'
      },
      substitution_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_DATE')
      },
      substitution_reason: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: 'Reason for substitution'
      },
      approval_status: {
        type: Sequelize.ENUM('PENDING', 'APPROVED', 'REJECTED', 'NOT_REQUIRED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      approved_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      approved_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      approval_remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'CANCELLED'),
        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('material_substitution_transaction', ['company_id', 'plant_id'], {
      name: 'material_substitution_txn_company_plant_idx'
    });
    await queryInterface.addIndex('material_substitution_transaction', ['reference_type', 'reference_id'], {
      name: 'material_substitution_txn_reference_idx'
    });
    await queryInterface.addIndex('material_substitution_transaction', ['original_material_id'], {
      name: 'material_substitution_txn_original_material_idx'
    });
    await queryInterface.addIndex('material_substitution_transaction', ['substitute_material_id'], {
      name: 'material_substitution_txn_substitute_material_idx'
    });
    await queryInterface.addIndex('material_substitution_transaction', ['approval_status'], {
      name: 'material_substitution_txn_approval_status_idx'
    });
    await queryInterface.addIndex('material_substitution_transaction', ['substitution_date'], {
      name: 'material_substitution_txn_date_idx'
    });
  },

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

