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;

public class ArchivePatientCarePlanService(
    IArchivePatientCarePlanRepository archivedPatientRepository,
    ILogger<ArchivePatientCarePlanService> logger,
    IHttpContextAccessor httpContextAccessor,
    ILocalAuditService auditService)
    : IArchivePatientCarePlanService
{
    #region Create

    public async Task<PatientCarePlan?> 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;
    }

    public async Task InsertManyAsync(List<PatientCarePlan> 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

    public async Task<List<PatientCarePlan>?> FindByPatientId(ObjectId patientId)
    {
        return await archivedPatientRepository.FindByPatientId(patientId);
    }

    public async Task<List<PatientCarePlan>?> FindByPatientId(string patientId)
    {
        return await archivedPatientRepository.FindByPatientId(patientId);
    }

    public async Task<List<PatientCarePlan>?> FindByPatientNumber(string patientId)
    {
        return await archivedPatientRepository.FindByPatientNumber(patientId);
    }

    public Task<List<PatientCarePlan>> FindAll()
    {
        return archivedPatientRepository.FindAll();
    }

    #endregion
}