module.exports = (sequelize, DataTypes) => {
  const ProductionCosting = sequelize.define('ProductionCosting', {
    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: true,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    finished_material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    production_costing_number: {
      type: DataTypes.STRING(80),
      allowNull: true
    },
    bom_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BOMHeader',
        key: 'id'
      }
    },
    routing_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Routing',
        key: 'id'
      }
    },
    material_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000
    },
    operation_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000
    },
    total_production_cost: {
      type: DataTypes.DECIMAL(18, 4),
      allowNull: false,
      defaultValue: 0.0000
    },
    material_cost_lines_json: {
      type: DataTypes.TEXT('long'),
      allowNull: true
    },
    operation_cost_lines_json: {
      type: DataTypes.TEXT('long'),
      allowNull: true
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    estimated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    estimated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'production_costing',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['finished_material_id']
      },
      {
        unique: true,
        fields: ['production_costing_number']
      },
      {
        fields: ['estimated_at']
      }
    ]
  });

  ProductionCosting.associate = function(models) {
    ProductionCosting.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionCosting.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionCosting.belongsTo(models.Material, { foreignKey: 'finished_material_id', as: 'finishedMaterial' });
    ProductionCosting.belongsTo(models.BOMHeader, { foreignKey: 'bom_id', as: 'bom' });
    ProductionCosting.belongsTo(models.Routing, { foreignKey: 'routing_id', as: 'routing' });
    ProductionCosting.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    ProductionCosting.belongsTo(models.User, { foreignKey: 'estimated_by', as: 'estimatedByUser' });
  };

  return ProductionCosting;
};
