using adas_core.Application.Repositories.Interfaces;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;

namespace adas_core.Infrastructure.Repositories;

/// <summary>
/// Represents a MongoDB-backed repository for managing <see cref="PatientCarePlan"/> entities,
/// providing concrete persistence operations defined by the <see cref="IPatientCarePlanRepository"/> contract.
/// </summary>
/// <typeparam name="PatientCarePlan">The type of the patient care plan entity managed by this repository.</typeparam>
public class PatientCarePlanRepository : MongoRepository<PatientCarePlan>, IPatientCarePlanRepository
{
    #region Properties

    private readonly ApiSettings _apiSettings;

    #endregion

    #region Update

    /// <summary>
    /// Asynchronously updates multiple object identifiers for a patient by delegating to the underlying asynchronous operation.
    /// </summary>
    /// <param name="patientid">The string identifier of the patient whose object identifiers will be updated.</param>
    /// <param name="patientId">The new <see cref="ObjectId"/> to assign to the patient's records.</param>
    /// <param name="oldId">The existing <see cref="ObjectId"/> to be replaced.</param>
    public async Task UpdateManyObjectId(string patientid, ObjectId patientId, ObjectId oldId)
        {
            await UpdateManyObjectIdAsync(patientid, patientId, oldId);
        }

    #endregion

    #region Constructor

    

    public PatientCarePlanRepository(
        IOptions<ApiSettings> apiSettings,
        IMongoDatabase database
    ) : base(database)
    {
        _apiSettings = apiSettings.Value;
    }

    #endregion

    #region Read

    /// <summary>
    /// Returns the collection name for patient care plan data, using the configured setting if available or a default fallback otherwise.
    /// </summary>
    /// <returns>The patient care plan collection name from <c>_apiSettings.PatientCarePlan</c>, or <c>"patients_care_plan"</c> when the setting is null.</returns>
    public override string GetCollectionName()
        {
            return _apiSettings.PatientCarePlan ?? "patients_care_plan";
        }

    /// <summary>
    /// Retrieves all care plans associated with the specified patient identifier from the data store.
    /// Returns an empty list when no matching care plans exist for the patient.
    /// </summary>
    /// <param name="patientId">The unique identifier of the patient whose care plans should be retrieved.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains a list of <see cref="PatientCarePlan"/> objects associated with the specified patient, or an empty list if none are found.</returns>
    public async Task<List<PatientCarePlan>> FindByPatientId(ObjectId patientId)
        {
            var result = await Collection.FindAsync(Builders<PatientCarePlan>.Filter.Eq(p => p.PatientId, patientId));
            return result.ToList();
        }

    /// <summary>
    /// Retrieves all patient care plans associated with the specified user identifier from the collection.
    /// </summary>
    /// <param name="userId">The unique identifier of the user whose patient care plans are being queried.</param>
    /// <returns>A list of <see cref="PatientCarePlan"/> instances matching the specified user identifier; an empty list is returned if no plans are found.</returns>
    public async Task<List<PatientCarePlan>> FindByUserId(ObjectId userId)
        {
            var result = await Collection.FindAsync(Builders<PatientCarePlan>.Filter.Eq(p => p.UserId, userId));
            return result.ToList();
        }

    /// <summary>
    /// Retrieves all patient care plans from the data store without applying any filter.
    /// </summary>
    /// <returns>A task that represents the asynchronous operation, containing a list of all <see cref="PatientCarePlan"/> records.</returns>
    public async Task<List<PatientCarePlan>> FindAll()
        {
            var result = await Collection.Find(Builders<PatientCarePlan>.Filter.Empty).ToListAsync();
            return result;
        }

    #endregion
}