using adas_core.Domain.Models; using adas_core.Domain.Models.Filter; using adas_core.Domain.Models.GroupedObservations; using adas_core.Domain.Models.MongoModels; using adas_core.Domain.Models.Responses; using MongoDB.Bson; using MongoDB.Driver; namespace adas_core.Application.Services.Interfaces; public interface IObservationService : IApiRequestService { //REMOVE /* List FindLastObservations(ObjectId patientId, string codingSystem, string code, int num = 2); */ /// /// Asynchronously retrieves all patient observations associated with the specified patient identifier. /// /// The unique identifier of the patient whose observations are to be retrieved. /// A task that returns an of instances for the given patient. Task> FindByPatientIdAsync(ObjectId patientId); /// /// Asynchronously retrieves patient observations matching the specified patient identifier, coding system, and name. /// /// The unique identifier of the patient whose observations are being queried. /// The coding system used to classify the observations (e.g., LOINC, SNOMED). /// The name of the observation to filter by. /// A task that represents the asynchronous operation, containing an of matching the criteria. Task> FindByPatientIdAndCodingSystemAsync(ObjectId patientId, string codingSystem, string name); /// /// Retrieves the most recent patient observations for the specified patient, optionally filtered by a set of observation codes. /// /// The unique identifier of the patient whose observations are being queried. /// The maximum number of most recent observations to return. Defaults to 2. /// An optional list of observation codes used to restrict the result set; if null, observations are not filtered by code. /// A task that resolves to a list of the most recent entries matching the criteria. Task> FindLastObservations(ObjectId patientId, int num = 2, List? filterObservations = null); /// /// Updates the status of the provided patient observations that have reached their expiration. /// /// The list of patient observations to update as expired. Task UpdateExpiredObservations(List expiredObservations); /// /// Asynchronously expires observations that are no longer valid and recalculates the dependent data. /// Task ExpireObservationsAndRecalculateAsync(); /// /// Asynchronously expires active alerts and powers off the device. /// Task ExpireAlertsAndPowerOffAsync(); /// /// Retrieves the most recent unique patient observations for the specified patient, filtered by observation name, with an optional cache expiration window in seconds. /// /// The unique identifier of the patient whose observations are being queried. /// The name of the observation to filter by. /// Optional expiration time in seconds applied to the cached results. If null, no expiration is applied. /// A task that represents the asynchronous operation. The task result contains a list of the latest unique values matching the specified patient and name. Task> FindLatestUniqueValuesByName(ObjectId patientId, string name, int? expires); /// /// Retrieves the most recent observations recorded for a patient, optionally filtered to a specific set of fields. /// /// The identifier of the patient whose observations are being retrieved. /// An optional list of fields used to restrict which observations are returned. When null, observations for all fields are considered. /// Indicates whether the returned observations should be mapped (default true) or returned in their raw form. /// The cancellation token used to cancel the asynchronous operation. /// A task that resolves to a list of the patient's most recent entries. Task> FindLastObservationsByField(ObjectId patientId, List? filterObservations = null, bool mapped = true, CancellationToken ct = default); /// /// Retrieves the most recent intravenous line observations associated with a specific location for the given patient. /// /// The unique identifier of the patient whose intravenous line observations are being queried. /// A task that represents the asynchronous operation. The task result contains a list of nullable entries representing the latest intravenous line observations by location, where individual entries may be null when no data is available. Task> FindLastIntravenousLinesObservationByLocation(ObjectId patientId); /// /// Asynchronously inserts a patient observation, with options to control whether the observation is persisted and whether it is mapped. /// /// The patient observation to insert. /// Indicates whether the observation should be persisted; defaults to true. /// Indicates whether the observation should be mapped; defaults to true. Task InsertObservation(PatientObservation patientObservation, bool persistObs = true, bool mapObs = true); /// /// Inserts the specified patient observation only if it has changed, optionally persisting the observation and applying a mapping during the insert. /// /// The name associated with the patient observation being evaluated for changes. /// The patient observation to compare against the existing value and potentially insert. /// Indicates whether the observation should be persisted when it is inserted. Defaults to true. /// Indicates whether the observation should be mapped as part of the insert operation. Defaults to true. /// A task that returns true if the observation was inserted because a change was detected; otherwise, false if no insert was performed. Task InsertIfChanged(string name, PatientObservation observation, bool persistObs = true, bool mapObs = true); /// /// Inserts a new nurse observation for a patient into the underlying data store. /// /// The patient observation data recorded by the nurse to be persisted. Task InsertNurseObservation(PatientObservation obs); /// /// Asynchronously deletes records associated with the specified patient identifier. /// /// The of the patient whose related records should be removed. Task DeleteByPatientId(ObjectId id); /// /// Asynchronously archives the specified patient observation, preserving it for historical or compliance purposes while removing it from the active set. /// /// The patient observation to archive. /// A task that represents the asynchronous archive operation. Task Archive(PatientObservation observation); /// /// Asynchronously maps a to a corresponding observation, optionally restricting the lookup to name-based matching. Returns when no matching observation is found. /// /// The source to be mapped. /// When , restricts the lookup to name-based matching; otherwise, the default mapping behavior is applied. /// A containing the mapped , or if no match is found. Task MapObservation(PatientObservation obs, bool onlyByName = false); /// /// Asynchronously retrieves the most recent observation time for each patient, returning a mapping of patient identifiers to their last observation timestamps. /// /// A task that represents the asynchronous operation. The task result contains a dictionary where each key is a patient and the associated value is the of that patient's latest observation. Task> FindAllLastPatientObservationTime(); /// /// Asynchronously maps or looks up a based on the name of the provided observation, returning the matching observation or null when no match is found. /// /// The whose name is used to perform the mapping or lookup. /// A that resolves to the matching , or null if no corresponding observation is found. Task MapObservationsByName(PatientObservation obs); /// /// Archives the specified patient, moving their record out of the active set so that it is retained for historical or compliance purposes while no longer appearing in routine operational queries. /// /// The patient whose record is to be archived. Task Archive(Patient patient); /// /// Archives records associated with the specified patient identifier. /// /// The unique identifier of the patient whose records should be archived. Task ArchiveByPatientId(ObjectId id); /// /// Updates an existing patient observation in the data store. /// /// The patient observation containing the updated information. /// A task that represents the asynchronous update operation. Task UpdateObservation(PatientObservation observation); /// /// Updates the specified identifier field () across multiple objects, replacing the existing value with the new value . /// /// The name of the identifier field to be updated. /// The new value to assign to the field. /// The current value to be replaced. /// A task that represents the asynchronous bulk update operation. Task UpdateManyObjectId(string nameId, ObjectId id, ObjectId oldId); /// /// Asynchronously broadcasts a patient observation to subscribed listeners or endpoints. /// /// The base patient observation to be broadcast. Task SendObsBroadcast(BasePatientObservation obs); /// /// Sends a broadcast containing the specified patient observations to the given patient location. /// /// The list of patient observations to include in the broadcast. /// The target patient location that will receive the broadcast. Task SendObsBroadcast(List obs, PatientLocation location); /// /// Asynchronously sends a broadcast of patient observations to the specified Point of Care (POC) system. /// /// The list of patient observations to be transmitted in the broadcast. /// The identifier of the Point of Care system that will receive the observations. Task SendObsBroadcast(List obs, ObjectId pocId); /// /// Asynchronously retrieves the most recent for a patient recorded before the specified date, optionally filtered by observation name. /// /// The unique identifier of the patient whose observation is being queried. /// The cutoff date; only observations recorded strictly before this date are considered. /// The optional name of the observation to filter by, or null to match any observation. /// A task that represents the asynchronous operation. The task result contains the latest matching , or null if none was found before the given date. Task FindLastBeforeDate(ObjectId patientId, DateTime date, string? obsName); /// /// Asynchronously retrieves the patient observations for the specified patient that share the given date, optionally filtered by observation name. /// /// The identifier of the patient whose observations will be searched. /// The date used to match observations. /// The optional observation name used to filter the results. When null, observations are not filtered by name. /// A task that represents the asynchronous operation. The task result contains a list of matching records, or null when no observations match the criteria. Task?> FindAnyWithSameDate(ObjectId patientId, DateTime date, string? obsName); //TODO To implement /// /// Retrieves all patient observations recorded before the specified date, optionally filtered to a specific set of observation types. /// /// The unique identifier of the patient whose observations are being queried. /// The cutoff date; only observations recorded before this date will be returned. /// An optional list of observation identifiers used to restrict the results to specific observation types. When null, all observation types are included. /// A task that resolves to a list of PatientObservation instances matching the criteria. Task> FindAllBeforeDate(ObjectId patientId, DateTime date, List? filterObservations = null); //TODO To implement /// /// Retrieves all entries for the specified patient recorded after the given date, optionally restricted to a subset of observation names. /// /// The identifier of the patient whose observations are being queried. /// The cutoff date; only observations with a timestamp after this value are returned. /// An optional list of observation names to restrict the result to. When null or empty, all observations after the date are returned. /// A that yields the list of matching entries. Task> FindAllAfterDate(ObjectId patientId, DateTime date, List? filterObservations = null); /// /// Retrieves a paginated list of patient observations for the specified patient within an optional date range, optionally filtered by observation names and including archived records when requested. /// /// The unique identifier of the patient whose observations are being queried. /// The inclusive lower bound of the observation date range, or null to apply no lower bound. /// The inclusive upper bound of the observation date range, or null to apply no upper bound. /// An optional list of observation names used to restrict the returned observations. /// When true, observations are retrieved from archived records; otherwise, only active records are considered. /// An optional pagination filter applied to the result set. /// A task that resolves to a list of entries matching the provided criteria. Task> FindAllBetweenDates(ObjectId patientId, DateTime? startDate, DateTime? endDate, List? filterObservations = null, bool fromArchived = false, PaginationFilter? filter = null); /// /// Asynchronously retrieves the most recent non-expired observations for the specified patient, optionally filtered by observation name and constrained by pagination parameters. /// /// The unique identifier of the patient whose observations are being queried. /// The name of the observation type used to filter the results. /// Optional parameter that defines the pagination boundary; when provided, observations are returned starting after this position. /// Optional parameter that limits the maximum number of observations returned. /// A task representing the asynchronous operation, containing a collection of matching non-expired records; an empty collection is returned if none are found. Task> FindLastNotExpiredObservatonsByPatient(ObjectId patientId, string name, int? endAfter = null, int? num = null); /// /// Checks observations and expires those that meet the expiration criteria. /// Task CheckAndExpireObservations(); /// /// Retrieves patient observations that have not been marked as expired but should be, based on their validity period or business rules. /// /// An asynchronous stream of instances that are not expired but meet the criteria to be expired. IAsyncEnumerable FindNotExpiredObservationsShouldBeExpired(); /// /// Processes a collection of patient observations, associating them with the specified patient and recording the message time. /// /// The list of patient observations to process. /// The patient associated with the observations. /// The timestamp of the message containing the observations. /// Optional additional data related to the observations. void ProcessObservations(List observations, Patient patient, DateTime messageTime, ObservationData? observationData = null); /// /// Asynchronously processes and expires observations that have exceeded their validity period. /// Task ExpireObservations(); /// /// Asynchronously inserts a simple patient observation record. /// /// The patient observation to insert. /// A task that represents the asynchronous insert operation. Task InsertSimpleObservation(PatientObservation observation); /// /// Asynchronously retrieves a paginated collection of patient observations based on the specified filter criteria. /// /// The pagination filter that controls the page size, page number, and any additional query criteria applied to the patient observations. /// A task that represents the asynchronous operation, containing a of with the requested page of results. Task> GetPaginatedObservations(PaginationFilter filter); /// /// Asynchronously saves a nurse observation request. /// /// The API request containing the nurse observation data to save. Task SaveRequestNurseObsAsync(ApiRequest request); }