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

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

			name: { type: DataTypes.STRING(100), allowNull: false },
			display_name: { type: DataTypes.STRING(100), allowNull: false },
			slug_name: { type: DataTypes.STRING(100), allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },
			code: { type: DataTypes.STRING(20), allowNull: true },
			type: { type: DataTypes.STRING(20), allowNull: true },
			is_parent: { type: DataTypes.BOOLEAN, allowNull: true },

			// fk
			parent_id: { type: DataTypes.INTEGER, allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			department_id: { type: DataTypes.INTEGER, allowNull: true },

			// sappsv1 or erpnext
			idx: { type: DataTypes.STRING, allowNull: true },
			ids: { type: DataTypes.INTEGER, allowNull: true },
			import_from: { type: DataTypes.STRING(100), allowNull: true },
		},
		{
			tableName: "designations",
			freezeTableName: true,
		},
	);

	/**
	 * Associates the Designation model with other models.
	 * @param {object} models - The models to associate with.
	 */
	Designation.associate = (models) => {
		/**
		 * Associates the Designation model with other models.
		 * @param {object} models - The models to associate with.
		 */
		Designation.belongsTo(models.Department, { foreignKey: "department_id", as: "department" });
		// self-referential association for parent designation
		Designation.belongsTo(models.Designation, { foreignKey: "parent_id", as: "parent" });

		/**
		 * A Designation belongs to a Company.
		 * Defines a many-to-one relationship between Designation and Company.
		 * @param {object} models - The models to associate with.
		 */
		Designation.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

		/**
		 * Associates the Designation model with the DesignationVersionChild model.
		 * A Designation has many DesignationVersionChilds.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.DesignationVersionChild, { foreignKey: "parent_id", as: "designationversionchilds" });

		/**
		 * Associates the Designation model with the WorkflowApprover model.
		 * A Designation has many WorkflowApprovers.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.WorkflowApprover, { foreignKey: "designation_id", as: "workflowapprovers" });

		/**
		 * Associates the Designation model with the EmployeeRequest model.
		 * A Designation has many EmployeeRequests where it is the required designation.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.EmployeeRequest, { foreignKey: "required_designation_id", as: "required_designation" });

		/**
		 * Associates the Designation model with the EmployeeRequest model.
		 * A Designation has many EmployeeRequests where it is the superior designation.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.EmployeeRequest, { foreignKey: "superior_designation_id", as: "superior_designation" });

		/**
		 * Associates the Designation model with the EmployeeRequest model.
		 * A Designation has many EmployeeRequests where it is the superior designation.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.EmployeeRequest, { foreignKey: "new_designation_id", as: "new_designation" });

		/**
		 * Associates the Designation model with the EmployeeRequest model.
		 * A Designation has many EmployeeRequests where it is the superior designation.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.Employee, { foreignKey: "designation_id", as: "employee" });

		/**
		 * Associates the Designation model with the JobPosting model.
		 * A Designation has many JobPosting where it is the JobPosting designation.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.JobPosting, { foreignKey: "designation_id", as: "jobposting" });

		/**
		 * Associates the Designation model with the TravelCostRate model.
		 * A Designation has many TravelCostRates where it is the designation of the TravelCostRate.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.TravelCostRate, { foreignKey: "designation_id", as: "travelcostrates" });

		/**
		 * Associates the Designation model with the UpCountry model.
		 * A Designation has many UpCountries where it is the designation of the UpCountry.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.UpCountry, { foreignKey: "designation_id", as: "upcountry" });

		/**
		 * Associates the Designation model with the UcCostRealization model.
		 * A Designation has many UcCostRealizations where it is the designation of the UcCostRealization.
		 * @param {object} models - The models to associate with.
		 */
		Designation.hasMany(models.UcCostRealization, { foreignKey: "designation_id", as: "uccostrealizations" });
	};

	return Designation;
};
