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

module.exports = (sequelize) => {
  const WorkActivityPlan = sequelize.define(
    "workactivityplan",
    {
      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, defaultValue: true },

      employee_id: { type: DataTypes.INTEGER, allowNull: true },
      company_id: { type: DataTypes.INTEGER, allowNull: true },
      period_month: { type: DataTypes.INTEGER, allowNull: false },
      period_year: { type: DataTypes.INTEGER, allowNull: false },
      work_type: { type: DataTypes.STRING, allowNull: true },
    },
    {
      tableName: "workactivityplans",
      freezeTableName: true,
      timestamps: true,
      createdAt: "createdAt",
      updatedAt: "updatedAt",
    }
  );

  WorkActivityPlan.associate = (models) => {
    WorkActivityPlan.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });
    WorkActivityPlan.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
    WorkActivityPlan.hasMany(models.WorkActivityPlanItem, {
      foreignKey: "work_activity_plan_id",
      as: "workactivityplanitems",
      onDelete: "CASCADE",
    });
  };

  return WorkActivityPlan;
};
