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

module.exports = (sequelize) => {
  const Province = sequelize.define(
    "province",
    {
      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 },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      code: { type: DataTypes.STRING(10), allowNull: true },
    },
    {
      tableName: "provinces",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Province model with other models.
   * @param {object} models - The models to associate with.
   */
  Province.associate = (models) => {
    /**
     * Associates the Province model with the City model.
     * Defines a one-to-many relationship between Province and City.
     * @param {object} models - The models to associate with.
     */
    Province.hasMany(models.City, { foreignKey: "province_id", as: "cities" });

    /**
     * Associates the Province model with the Client model.
     * Defines a one-to-many relationship between Province and Client.
     * @param {object} models - The models to associate with.
     */
    Province.hasMany(models.Client, { foreignKey: "province_id", as: "clients" });

    /**
     * Associates the Province model with the Outlet model.
     * Defines a one-to-many relationship between Province and Outlet.
     * @param {object} models - The models to associate with.
     */
    Province.hasMany(models.Outlet, { foreignKey: "province_id", as: "outlets" });

    /**
     * Associates the Province model with the EmployeeRequest model.
     * Defines a one-to-many relationship between Province and EmployeeRequest.
     * @param {object} models - The models to associate with.
     */
    Province.hasMany(models.EmployeeRequest, { foreignKey: "province_id", as: "employeerequests" });
    /**
     * Associates the Province model with the JobPosting model.
     * Defines a one-to-many relationship between Province and JobPosting.
     * @param {object} models - The models to associate with.
     */
    Province.hasMany(models.JobPosting, { foreignKey: "province_id", as: "jobpostings" });
  };

  return Province;
};
