module.exports = (sequelize, DataTypes) => {
  const ProductionCost = sequelize.define('ProductionCost', {
    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'
      }
    },
    cost_type: {
      type: DataTypes.ENUM('STANDARD', 'ACTUAL', 'ESTIMATED'),
      allowNull: false,
      defaultValue: 'STANDARD',
      comment: 'Type of cost calculation'
    },
    material_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Total material cost'
    },
    labor_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Total labor cost'
    },
    overhead_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Total overhead cost'
    },
    setup_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Setup cost'
    },
    total_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Total production cost'
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      comment: 'Quantity for which cost is calculated'
    },
    unit_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000,
      comment: 'Cost per unit'
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    cost_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CostCenter',
        key: 'id'
      },
      comment: 'Cost center for cost allocation'
    },
    calculated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: 'When cost was calculated'
    },
    calculated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'production_cost',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['cost_type']
      },
      {
        fields: ['cost_center_id']
      }
    ]
  });

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

  return ProductionCost;
};

