const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const UcCostRealization = sequelize.define(
    "uccostrealization",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },

      upcountry_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "upcountries", key: "id" } },
      category: { type: DataTypes.STRING(150), allowNull: true },
      upload_date: { type: DataTypes.DATEONLY, allowNull: true },
      amount: { type: DataTypes.DECIMAL(10, 2), allowNull: true },
      budget: { type: DataTypes.DECIMAL(10, 2), allowNull: true },
      filename: { type: DataTypes.STRING(150), allowNull: true },
      note: { type: DataTypes.STRING(250), allowNull: true },
      status: { type: DataTypes.STRING(250), allowNull: true },

      territory_id: { type: DataTypes.INTEGER, allowNull: true },
      designation_id: { type: DataTypes.INTEGER, allowNull: true },
      employee_id: { type: DataTypes.INTEGER, allowNull: true },
    },
    {
      tableName: "uccostrealizations",
      freezeTableName: true,
    }
  );

  /**
   * Associates the UcCostRealization model with other models.
   * @param {object} models - The models to associate with.
   * @description
   */
  UcCostRealization.associate = (models) => {
    UcCostRealization.belongsTo(models.UpCountry, { foreignKey: "upcountry_id", as: "upcountry", onDelete: "CASCADE" });
    UcCostRealization.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });
    UcCostRealization.belongsTo(models.Designation, { foreignKey: "designation_id", as: "designation" });
    UcCostRealization.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });
  };

  return UcCostRealization;
};
