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

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

			date: { type: DataTypes.DATEONLY, allowNull: false },
			in_time: { type: DataTypes.TIME, allowNull: true },
			out_time: { type: DataTypes.TIME, allowNull: true },
			in_status: { type: DataTypes.STRING, allowNull: true },
			out_status: { type: DataTypes.STRING, allowNull: true },
			in_distance: { type: DataTypes.STRING, allowNull: true },
			out_distance: { type: DataTypes.STRING, allowNull: true },
			in_address: { type: DataTypes.TEXT, allowNull: true },
			out_address: { type: DataTypes.TEXT, allowNull: true },
			total_hour: { type: DataTypes.FLOAT, allowNull: true },
			in_latitude: { type: DataTypes.STRING, allowNull: true },
			in_longitude: { type: DataTypes.STRING, allowNull: true },
			out_latitude: { type: DataTypes.STRING, allowNull: true },
			out_longitude: { type: DataTypes.STRING, allowNull: true },
			reason_type: { type: DataTypes.STRING(100), allowNull: true },
			reason: { type: DataTypes.TEXT, allowNull: true },
			notes: { type: DataTypes.TEXT, 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 },
			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 },

			// foreign keys
			company_id: { type: DataTypes.INTEGER, allowNull: false },
			department_id: { type: DataTypes.INTEGER, allowNull: true },
			employee_id: { type: DataTypes.INTEGER, allowNull: false },
			shifttype_id: { type: DataTypes.INTEGER, allowNull: true },

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

	/**
	 * Associates the AttendanceRequest model with other models.
	 * @param {object} models - The models to associate with.
	 */
	AttendanceRequest.associate = (models) => {
		AttendanceRequest.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });

		AttendanceRequest.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

		AttendanceRequest.belongsTo(models.ShiftType, { foreignKey: "shifttype_id", as: "shifttype" });

		AttendanceRequest.belongsTo(models.Department, { foreignKey: "department_id", as: "department" });

		AttendanceRequest.belongsTo(models.HolidayListItem, { foreignKey: "holidaylistitem_id", as: "holidaylistitem" });

		AttendanceRequest.hasMany(models.HistoryAttendanceRequest, { foreignKey: "attendancerequest_id", as: "historyattendancerequests" });

		AttendanceRequest.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });

		AttendanceRequest.belongsTo(models.Employee, { foreignKey: "leave_approver", as: "leaveapprover_name" });
	};

	return AttendanceRequest;
};
