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

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

			// main
			name: { type: DataTypes.STRING(150), allowNull: true },
			display_name: { type: DataTypes.STRING(150), allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },

			from_date: { type: DataTypes.DATEONLY, allowNull: false },
			to_date: { type: DataTypes.DATEONLY, allowNull: false },
			half_day: { type: DataTypes.BOOLEAN, allowNull: true },
			half_day_date: { type: DataTypes.DATEONLY, allowNull: true },

			total_leave_days: { type: DataTypes.FLOAT, allowNull: false },
			leave_balance: { type: DataTypes.FLOAT, allowNull: false },
			estimated_leave_balance: { type: DataTypes.FLOAT, allowNull: false },
			reason: { type: DataTypes.TEXT, allowNull: true },
			filename: { type: DataTypes.STRING, allowNull: true },
			use_doctor_letter: { type: DataTypes.BOOLEAN, allowNull: true },
			is_lwp: { type: DataTypes.BOOLEAN, allowNull: true },
			is_sl: { type: DataTypes.BOOLEAN, allowNull: true },

			follow_via_email: { type: DataTypes.BOOLEAN, allowNull: true },
			leave_approver: { type: DataTypes.INTEGER, allowNull: true },
			posting_date: { type: DataTypes.DATE, allowNull: true },
			submitted_date: { type: DataTypes.DATE, allowNull: true },
			approver_date: { type: DataTypes.DATE, allowNull: true },
			status: { type: DataTypes.STRING, allowNull: true },

			// additional
			employee_name: { type: DataTypes.STRING, allowNull: true },
			leavetype_name: { type: DataTypes.STRING(150), allowNull: true },
			shifttype_id: { type: DataTypes.INTEGER, allowNull: true },
			employee_email: { type: DataTypes.STRING, allowNull: true },
			leave_approver_email: { type: DataTypes.STRING, allowNull: true },
			leave_approver_name: { type: DataTypes.STRING, allowNull: true },
			leave_approver_designation_territory: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			employee_designation_territory: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			cancel_reason: { type: DataTypes.TEXT, allowNull: true },
			revision_notes: { type: DataTypes.TEXT, allowNull: true },
			rejection_reason: { type: DataTypes.TEXT, allowNull: true },

			// New field for workflow integration (added via migration)
			workflow_id: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null },

			//fk
			employee_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			department_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
			},
			leavetype_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			leaveallocation_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
			},
		},
		{
			tableName: "leaveapplications",
			freezeTableName: true,
		},
	);

	/**
	 * Associates the LeaveApplication model with other models.
	 * @param {object} models - The models to associate with.
	 */
	LeaveApplication.associate = (models) => {
		LeaveApplication.belongsTo(models.LeaveType, { foreignKey: "leavetype_id", as: "leavetype" });
		LeaveApplication.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		LeaveApplication.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });
		LeaveApplication.belongsTo(models.Department, { foreignKey: "department_id", as: "department" });
		LeaveApplication.belongsTo(models.LeaveAllocation, { foreignKey: "leaveallocation_id", as: "leaveallocation" });
		LeaveApplication.hasMany(models.HistoryLeaveApplication, { foreignKey: "leaveapplication_id", as: "historyleaveapplications" });
		LeaveApplication.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		LeaveApplication.belongsTo(models.Employee, { foreignKey: "leave_approver", as: "leaveapprover_name" });
	};

	return LeaveApplication;
};
