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

module.exports = (sequelize) => {
  const City = sequelize.define(
    "citie",
    {
      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 },

      // fk
      province_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "provinces",
          key: "id",
        },
      },
    },
    {
      tableName: "cities",
      freezeTableName: true,
    }
  );

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

    /**
     * A City has many Clients.
     * Defines a one-to-many relationship between City and Client.
     * @param {object} models - The models to associate with.
     */
    City.hasMany(models.Client, { foreignKey: "city_id", as: "clients" });

    /**
     * A City has many Outlets.
     * Defines a one-to-many relationship between City and Outlet.
     * @param {object} models - The models to associate with.
     */
    City.hasMany(models.Outlet, { foreignKey: "city_id", as: "outlets" });

    /**
     * A City has many Employee Requests.
     * Defines a one-to-many relationship between City and EmployeeRequest.
     * @param {object} models - The models to associate with.
     */
    City.hasMany(models.EmployeeRequest, { foreignKey: "city_id", as: "employeerequests" });
    /**
     * A City has many Employee Requests.
     * Defines a one-to-many relationship between City and JobPosting.
     * @param {object} models - The models to associate with.
     */
    City.hasMany(models.JobPosting, { foreignKey: "city_id", as: "jobpostings" });
  };

  return City;
};
