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

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

			name: { type: DataTypes.STRING(100), allowNull: false },
			display_name: { type: DataTypes.STRING(200), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },
			code: { type: DataTypes.STRING(10), allowNull: true },

			max_leaves_allowed: { type: DataTypes.FLOAT, allowNull: true },
			applicable_after: { type: DataTypes.DATEONLY, allowNull: true },
			max_continuous_days_allowed: { type: DataTypes.FLOAT, allowNull: true },
			is_carry_forward: { type: DataTypes.BOOLEAN, allowNull: true },
			is_lwp: { type: DataTypes.BOOLEAN, allowNull: true },
			is_sl: { type: DataTypes.BOOLEAN, allowNull: true },
			is_ppl: { type: DataTypes.BOOLEAN, allowNull: true },
			is_optional_leave: { type: DataTypes.BOOLEAN, allowNull: true },
			allow_negative: { type: DataTypes.BOOLEAN, allowNull: true },
			allow_over_allocation: { type: DataTypes.BOOLEAN, allowNull: true },
			is_compensatory: { type: DataTypes.BOOLEAN, allowNull: true },
			maximum_carry_forwarded_leaves: {
				type: DataTypes.FLOAT,
				allowNull: true,
			},
			encashment_threshold_days: { type: DataTypes.FLOAT, allowNull: true },
			is_earned_leave: { type: DataTypes.BOOLEAN, allowNull: true },

			//fk
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
		},
		{
			tableName: "leavetypes",
			freezeTableName: true,
		}
	);

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

		LeaveType.hasMany(models.LeaveAllocation, {
			foreignKey: "leavetype_id",
			as: "leaveallocations",
		});
	};

	return LeaveType;
};
