module.exports = (sequelize, DataTypes) => {
  const ProductionCostVariance = sequelize.define('ProductionCostVariance', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    standard_cost_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionCost',
        key: 'id'
      }
    },
    actual_cost_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionCost',
        key: 'id'
      }
    },
    material_variance: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Material cost variance (Actual - Standard)'
    },
    labor_variance: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Labor cost variance (Actual - Standard)'
    },
    overhead_variance: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Overhead cost variance (Actual - Standard)'
    },
    setup_variance: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Setup cost variance (Actual - Standard)'
    },
    total_variance: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Total cost variance (Actual - Standard)'
    },
    variance_percentage: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      comment: 'Variance as percentage of standard cost'
    },
    variance_type: {
      type: DataTypes.ENUM('FAVORABLE', 'UNFAVORABLE', 'NEUTRAL'),
      allowNull: false,
      defaultValue: 'NEUTRAL',
      comment: 'Favorable = actual < standard, Unfavorable = actual > standard'
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      comment: 'Quantity for variance calculation'
    },
    unit_variance: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Variance per unit'
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    calculated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    calculated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'production_cost_variance',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['variance_type']
      }
    ]
  });

  ProductionCostVariance.associate = function(models) {
    ProductionCostVariance.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionCostVariance.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionCostVariance.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionCostVariance.belongsTo(models.ProductionCost, { foreignKey: 'standard_cost_id', as: 'standardCost' });
    ProductionCostVariance.belongsTo(models.ProductionCost, { foreignKey: 'actual_cost_id', as: 'actualCost' });
    ProductionCostVariance.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    ProductionCostVariance.belongsTo(models.User, { foreignKey: 'calculated_by', as: 'calculatedByUser' });
  };

  return ProductionCostVariance;
};

