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

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

			from_date: { type: DataTypes.DATEONLY, allowNull: false },
			to_date: { type: DataTypes.DATEONLY, allowNull: false },
			half_day: { type: DataTypes.BOOLEAN, allowNull: false },
			half_day_date: { type: DataTypes.DATEONLY, allowNull: true },
			reason: { type: DataTypes.TEXT, allowNull: false },
			explanation: { type: DataTypes.TEXT, allowNull: true },
			notes: { type: DataTypes.TEXT, allowNull: true },
			filename: { type: DataTypes.STRING, allowNull: true },
			half_day_type: { type: DataTypes.STRING(100), allowNull: true },

			//fk
			employee_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
				references: {
					model: "employees",
					key: "id",
				},
			},
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
				references: {
					model: "companies",
					key: "id",
				},
			},
			attendance_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
			},
		},
		{
			tableName: "permiteases",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the PermitEase model with other models.
	 * @param {object} models - The models to associate with.
	 */
	PermitEase.associate = (models) => {
		/**
		 * Associates the PermitEase model with the Company model.
		 * Defines a many-to-one relationship between PermitEase and Company.
		 * @param {object} models - The models to associate with.
		 * @property {object} company - The Company model associated with PermitEase.
		 */
		PermitEase.belongsTo(models.Company, {
			foreignKey: "company_id",
			as: "company",
		});

		/**
		 * Associates the PermitEase model with the Employee model.
		 * Defines a many-to-one relationship between PermitEase and Employee.
		 * @param {object} models - The models to associate with.
		 * @property {object} employee - The Employee model associated with PermitEase.
		 */
		PermitEase.belongsTo(models.Employee, {
			foreignKey: "employee_id",
			as: "employee",
		});
	};

	return PermitEase;
};
