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

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

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      description: { type: DataTypes.TEXT, allowNull: true },
    },
    {
      tableName: "years",
      freezeTableName: true,
    }
  );

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

  return Year;
};
