'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('production_cost_variance', {
      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'
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      standard_cost_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_cost',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      actual_cost_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_cost',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      material_variance: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Material cost variance (Actual - Standard)'
      },
      labor_variance: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Labor cost variance (Actual - Standard)'
      },
      overhead_variance: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Overhead cost variance (Actual - Standard)'
      },
      setup_variance: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Setup cost variance (Actual - Standard)'
      },
      total_variance: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Total cost variance (Actual - Standard)'
      },
      variance_percentage: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        comment: 'Variance as percentage of standard cost'
      },
      variance_type: {
        type: Sequelize.ENUM('FAVORABLE', 'UNFAVORABLE', 'NEUTRAL'),
        allowNull: false,
        defaultValue: 'NEUTRAL',
        comment: 'Favorable = actual < standard, Unfavorable = actual > standard'
      },
      quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        comment: 'Quantity for variance calculation'
      },
      unit_variance: {
        type: Sequelize.DECIMAL(18, 4),
        allowNull: false,
        defaultValue: 0.0000,
        comment: 'Variance per unit'
      },
      currency_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'currency',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      calculated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      calculated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      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('production_cost_variance', ['company_id', 'plant_id'], {
      name: 'production_cost_variance_company_plant_idx'
    });
    await queryInterface.addIndex('production_cost_variance', ['production_order_id'], {
      name: 'production_cost_variance_po_idx'
    });
    await queryInterface.addIndex('production_cost_variance', ['variance_type'], {
      name: 'production_cost_variance_type_idx'
    });
  },

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

