using adas_core.Application.Repositories.Interfaces;
using adas_core.Application.Services.Interfaces;
using adas_core.Domain.Models.MongoModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
namespace adas_core.Application.Services;
///
/// Implements to provide the application service responsible for archiving patient care plans.
/// Collaborates with for data access, for diagnostics, for HTTP context retrieval, and for local auditing.
///
///
/// The collaborators are supplied through the primary constructor, allowing the service to fulfill the contract defined by .
///
///
public class ArchivePatientCarePlanService(
IArchivePatientCarePlanRepository archivedPatientRepository,
ILogger logger,
IHttpContextAccessor httpContextAccessor,
ILocalAuditService auditService)
: IArchivePatientCarePlanService
{
#region Create
///
/// Inserts a patient care plan into the archived patient repository, logs the operation, and creates an audit log entry capturing the current user context.
///
/// The patient care plan to be archived and inserted.
/// The inserted patient care plan, or if no plan was provided.
///
public async Task InsertOneAsync(PatientCarePlan patient)
{
await archivedPatientRepository.InsertOneAsync(patient);
logger.LogInformation(
"Inserted archived patient care plan for patientId: {PatientId}, patientNumber: {PatientNumber}",
patient.PatientId, patient.PatientNumber);
await auditService.CreateAuditLogAsync(httpContextAccessor.HttpContext?.User!, null, patient);
return patient;
}
///
/// Asynchronously inserts a batch of archived patient care plans into the repository and creates an audit log entry for each one using the current HTTP context user.
///
/// The list of patient care plans to insert and audit.
///
public async Task InsertManyAsync(List patientCarePla)
{
await archivedPatientRepository.InsertManyAsync(patientCarePla);
logger.LogInformation("Inserted {Count} archived patient care plans", patientCarePla.Count);
foreach (var patient in patientCarePla)
await auditService.CreateAuditLogAsync(httpContextAccessor.HttpContext?.User!, null, patient);
}
#endregion
#region Read
///
/// Retrieves the list of archived patient care plans associated with the specified patient identifier by delegating to the underlying repository.
/// Returns a nullable list, which may be null when no archived care plans exist for the patient.
///
/// The unique identifier of the patient whose archived care plans are being searched.
/// A task that represents the asynchronous operation. The task result contains a list of for the patient, or null if no records are found.
///
public async Task?> FindByPatientId(ObjectId patientId)
{
return await archivedPatientRepository.FindByPatientId(patientId);
}
///
/// Retrieves a list of archived patient care plans associated with the specified patient identifier.
/// Returns null when no archived care plans are found for the given patient.
///
/// The unique identifier of the patient whose archived care plans are being retrieved.
/// A list of entries for the patient, or null if no records exist.
///
public async Task?> FindByPatientId(string patientId)
{
return await archivedPatientRepository.FindByPatientId(patientId);
}
///
/// Retrieves a list of archived patient care plans associated with the specified patient number.
/// Returns null if no archived care plans are found for the given patient.
///
/// The unique identifier of the patient whose archived care plans are being searched.
/// A list of objects for the specified patient, or null if no records are found.
///
public async Task?> FindByPatientNumber(string patientId)
{
return await archivedPatientRepository.FindByPatientNumber(patientId);
}
///
/// Retrieves all archived patient care plans by delegating to the archived patient repository.
///
/// A task that resolves to a list of objects representing all archived patient care plans.
///
public Task> FindAll()
{
return archivedPatientRepository.FindAll();
}
#endregion
}