'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_valuation', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      valuation_area: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Valuation area (usually same as plant)'
      },
      valuation_type: {
        type: Sequelize.ENUM('S', 'V', 'T'),
        allowNull: false,
        defaultValue: 'S',
        comment: 'S=Standard, V=Moving Average, T=Periodic Unit Price'
      },
      price_control: {
        type: Sequelize.ENUM('S', 'V'),
        allowNull: false,
        defaultValue: 'S',
        comment: 'S=Standard Price, V=Moving Average Price'
      },
      standard_price: {
        type: Sequelize.DECIMAL(15, 4),
        allowNull: true,
        defaultValue: 0.0000
      },
      moving_average_price: {
        type: Sequelize.DECIMAL(15, 4),
        allowNull: true,
        defaultValue: 0.0000
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      valuation_class: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      costing_lot_size: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        defaultValue: 1.000
      },
      original_price: {
        type: Sequelize.DECIMAL(15, 4),
        allowNull: true,
        defaultValue: 0.0000
      },
      last_price_change_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        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: {
        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('material_valuation', ['material_id', 'plant_id', 'valuation_area'], { unique: true });
    await queryInterface.addIndex('material_valuation', ['plant_id']);
    await queryInterface.addIndex('material_valuation', ['status']);
    await queryInterface.addIndex('material_valuation', ['effective_from', 'effective_to']);
  },

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

