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

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

            no_fpk: { type: DataTypes.STRING, allowNull: true },
            request_date: { type: DataTypes.DATE, allowNull: true },
            request_reason: { type: DataTypes.INTEGER, allowNull: true },
            people_amount: { type: DataTypes.INTEGER, allowNull: true },
            employee_status: { type: DataTypes.STRING, allowNull: true },
            status_reason: { type: DataTypes.TEXT, allowNull: true },
            request_from: { type: DataTypes.STRING, allowNull: true },
            internal_note: { type: DataTypes.TEXT, allowNull: true },
            workplace_type: { type: DataTypes.STRING, allowNull: true },
            gender: { type: DataTypes.STRING, allowNull: true },
            age_min: { type: DataTypes.INTEGER, allowNull: true },
            age_max: { type: DataTypes.INTEGER, allowNull: true },
            education: { type: DataTypes.JSON, allowNull: true },
            major: { type: DataTypes.JSON, allowNull: true },
            min_gpa: { type: DataTypes.INTEGER, allowNull: true },
            max_gpa: { type: DataTypes.INTEGER, allowNull: true },
            experience: { type: DataTypes.TEXT, allowNull: true },
            general_skill: { type: DataTypes.TEXT, allowNull: true },
            special_skill: { type: DataTypes.TEXT, allowNull: true },
            job_desc: { type: DataTypes.TEXT, allowNull: true },
            other: { type: DataTypes.TEXT, allowNull: true },
            min_salary: { type: DataTypes.INTEGER, allowNull: true },
            max_salary: { type: DataTypes.INTEGER, allowNull: true },
            visible_salary: { type: DataTypes.BOOLEAN, allowNull: true },
            due_date: { type: DataTypes.DATE, allowNull: true },
            priority: { type: DataTypes.STRING, allowNull: true },
            
            submit_date: { type: DataTypes.DATE, allowNull: true },
            approve_date: { type: DataTypes.DATE, allowNull: true },
            hired_date: { type: DataTypes.DATE, allowNull: true },
            
            doc_status: { type: DataTypes.INTEGER, allowNull: true },
            chat: { type: DataTypes.JSON, allowNull: true },

            hal: { type: DataTypes.STRING, allowNull: true },
            hal_date: { type: DataTypes.DATE, allowNull: true },
            hal_reason: { type: DataTypes.TEXT, allowNull: true },
            new_division: { type: DataTypes.STRING, allowNull: true },
            
            // fk 
            internal_employee_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'employees', key: 'id' } },
            replace_employee_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'employees', key: 'id' } },
            department_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'departments', key: 'id' } },
            required_designation_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'designations', key: 'id' } },
            superior_designation_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'designations', key: 'id' } },
            province_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'provinces', key: 'id' } },
            city_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'cities', key: 'id' } },
            faculty_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'faculties', key: 'id' } },
            company_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'companies', key: 'id' } },
            
            new_designation_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'designations', key: 'id' } },
            new_territory_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'territories', key: 'id' } },
            
            // sappsv1 or erpnext
            ids: { type: DataTypes.INTEGER, allowNull: true },
            import_from: { type: DataTypes.STRING(10), allowNull: true },
        },
        {
            tableName: "employeerequests",
            freezeTableName: true,
        }
    );


    EmployeeRequest.associate = (models) => {
        EmployeeRequest.belongsTo(models.User, { foreignKey: 'owner', as: 'user' });
        EmployeeRequest.belongsTo(models.Employee, { foreignKey: 'replace_employee_id', as: 'replace_employee' });
        EmployeeRequest.belongsTo(models.Employee, { foreignKey: 'internal_employee_id', as: 'internal_employee' });
        EmployeeRequest.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
        EmployeeRequest.belongsTo(models.Designation, { foreignKey: 'required_designation_id', as: 'required_designation' });
        EmployeeRequest.belongsTo(models.Designation, { foreignKey: 'superior_designation_id', as: 'superior_designation' });
        EmployeeRequest.belongsTo(models.Designation, { foreignKey: 'new_designation_id', as: 'new_designation' });
        EmployeeRequest.belongsTo(models.Territory, { foreignKey: 'new_territory_id', as: 'new_territory' });
        EmployeeRequest.belongsTo(models.Province, { foreignKey: 'province_id', as: 'province' });
        EmployeeRequest.belongsTo(models.City, { foreignKey: 'city_id', as: 'city' });
        EmployeeRequest.belongsTo(models.Faculty, { foreignKey: 'faculty_id', as: 'faculty' });
        EmployeeRequest.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
        EmployeeRequest.hasMany(models.Applicant, { foreignKey: 'employeerequest_id', as: 'applicants' });
        EmployeeRequest.hasMany(models.JobPostingChild, { foreignKey: 'employeerequest_id', as: 'jobpostingchilds' });
    }

    return EmployeeRequest;
};
