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

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

      applicant_status: { type: DataTypes.STRING(100), allowNull: true },
      status_date: { type: DataTypes.DATE, allowNull: true },
      first_name: { type: DataTypes.STRING(100), allowNull: false },
      middle_name: { type: DataTypes.STRING(100), allowNull: true },
      last_name: { type: DataTypes.STRING(100), allowNull: true },
      name: { type: DataTypes.STRING(100), allowNull: false },
      id_number: { type: DataTypes.STRING(100), allowNull: true },
      religion: { type: DataTypes.STRING(50), allowNull: true },
      gender: { type: DataTypes.STRING(50), allowNull: true },
      is_pregnant: { type: DataTypes.BOOLEAN, allowNull: true },
      week_pregnancy: { type: DataTypes.INTEGER, allowNull: true },
      province: { type: DataTypes.STRING(50), allowNull: true },
      birth_place: { type: DataTypes.STRING(50), allowNull: true },
      date_of_birth: { type: DataTypes.DATE, allowNull: true },
      id_address: { type: DataTypes.JSON, allowNull: true },
      current_address: { type: DataTypes.JSON, allowNull: true },
      residence_status: { type: DataTypes.STRING(50), allowNull: true },
      email: { type: DataTypes.STRING(50), allowNull: true },
      mobile_no: { type: DataTypes.STRING(50), allowNull: true },
      marital_status: { type: DataTypes.STRING(50), allowNull: true },
      marital_date: { type: DataTypes.STRING(50), allowNull: true },
      driving_license: { type: DataTypes.JSON, allowNull: true },
      father_info: { type: DataTypes.JSON, allowNull: true },
      mother_info: { type: DataTypes.JSON, allowNull: true },
      sibling_info: { type: DataTypes.JSON, allowNull: true },
      spouse_info: { type: DataTypes.JSON, allowNull: true },
      children_info: { type: DataTypes.JSON, allowNull: true },
      education_info: { type: DataTypes.JSON, allowNull: true },
      thesis_title: { type: DataTypes.STRING(100), allowNull: true },
      last_company: { type: DataTypes.JSON, allowNull: true },
      work_answer: { type: DataTypes.JSON, allowNull: true },
      training_info: { type: DataTypes.JSON, allowNull: true },
      foreign_language: { type: DataTypes.JSON, allowNull: true },
      hobby: { type: DataTypes.STRING(100), allowNull: true },
      expected_salary: { type: DataTypes.INTEGER, allowNull: true },
      reference: { type: DataTypes.JSON, allowNull: true },
      work_interest: { type: DataTypes.JSON, allowNull: true },
      interest_reason: { type: DataTypes.TEXT, allowNull: true },
      company_answer: { type: DataTypes.JSON, allowNull: true },
      cv: { type: DataTypes.TEXT, allowNull: true },
      psikotest_file: { type: DataTypes.TEXT, allowNull: true },
      rating: { type: DataTypes.STRING(100), allowNull: true },
      rating_note: { type: DataTypes.TEXT, allowNull: true },
      email_subject: { type: DataTypes.TEXT, allowNull: true },
      email_text: { type: DataTypes.TEXT, allowNull: true },
      interview_info: { type: DataTypes.JSON, allowNull: true },
      interview_info_user1: { type: DataTypes.JSON, allowNull: true },
      interview_info_user2: { type: DataTypes.JSON, allowNull: true },
      survey: { type: DataTypes.STRING(100), allowNull: true },
      applicant_join_date: { type: DataTypes.DATE, allowNull: true },
      blood_type: { type: DataTypes.STRING(100), allowNull: true },
      emergency_no: { type: DataTypes.STRING(100), allowNull: true },
      emergency_status: { type: DataTypes.STRING(100), allowNull: true },
      review: { type: DataTypes.BOOLEAN, allowNull: true },

      //   fk
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "companies",
          key: "id",
        },
      },
      employeerequest_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "employeerequests",
          key: "id",
        },
      },
      jobposting_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: {
          model: "jobpostings",
          key: "id",
        },
      },
    },
    {
      tableName: "applicants",
      freezeTableName: true,
    }
  );

  /**
   * Associates the Applicant model with other models.
   * @param {object} models - The models to associate with.
   */
  Applicants.associate = (models) => {
    /**
     * Associates the Applicants model with the Company model.
     * A Udk belongs to a Company.
     * @param {object} models - The models to associate with.
     */
    Applicants.belongsTo(models.Company, {
      foreignKey: "company_id",
      as: "company",
    });

    Applicants.belongsTo(models.EmployeeRequest, {
      foreignKey: "employeerequest_id",
      as: "employeerequest",
    });
    Applicants.belongsTo(models.JobPosting, {
      foreignKey: "jobposting_id",
      as: "jobposting",
    });

    Applicants.belongsTo(models.User, { foreignKey: 'modified_by', as: 'modifier' });
    Applicants.hasMany(models.JobPostingChild, { foreignKey: 'applicant_id', as: 'jobpostingchilds' });

  };

  return Applicants;
};
