using adas_core.Domain.Models.Pumps;
using MongoDB.Bson;

namespace adas_core.Application.Repositories.Interfaces;

public interface IPumpAlarmEventRepository
{
    /// <summary>
    /// Asynchronously inserts a pump alarm event into the underlying data store.
    /// </summary>
    /// <param name="alarmEvent">The pump alarm event to be persisted.</param>
    Task InsertAsync(PumpAlarmEvent alarmEvent);

    /// <summary>
    /// Retrieves pump alarm events associated with the specified device, optionally filtered by a date range and capped by a maximum result count.
    /// </summary>
    /// <param name="deviceId">The unique identifier of the device whose alarm events should be returned.</param>
    /// <param name="from">Optional start of the date range used to filter the events.</param>
    /// <param name="to">Optional end of the date range used to filter the events.</param>
    /// <param name="limit">Optional maximum number of alarm events to return.</param>
    /// <returns>A task that represents the asynchronous operation, containing the collection of pump alarm events matching the criteria.</returns>
    Task<IEnumerable<PumpAlarmEvent>> FindByDeviceIdAsync(
            string deviceId,
            DateTime? from = null,
            DateTime? to = null,
            int? limit = null);

    /// <summary>
    /// Asynchronously retrieves the most recent pump alarm event associated with the specified device identifier.
    /// Returns null if no alarm event is found for the given device.
    /// </summary>
    /// <param name="deviceId">The unique identifier of the device whose last pump alarm event is to be retrieved.</param>
    /// <returns>A task that represents the asynchronous operation, containing the most recent <see cref="PumpAlarmEvent"/> for the device, or null if none exists.</returns>
    Task<PumpAlarmEvent?> FindLastByDeviceIdAsync(string deviceId);

    // cleanup
    /// <summary>
    /// Deletes records associated with the specified patient identifier.
    /// </summary>
    /// <param name="patientId">The unique identifier of the patient whose records should be deleted.</param>
    Task DeleteByPatientId(ObjectId patientId);
    
    /// <summary>
    /// Asynchronously updates the ObjectId value of multiple records where the specified field currently matches the old identifier, replacing it with the new identifier.
    /// </summary>
    /// <param name="fieldName">The name of the field whose value should be updated.</param>
    /// <param name="newId">The new ObjectId value to assign to the matching records.</param>
    /// <param name="oldId">The existing ObjectId value used to identify the records to be updated.</param>
    /// <returns>A task that represents the asynchronous operation, containing the number of records that were updated.</returns>
    Task<long> UpdateManyObjectIdByFiledNameAsync(string fieldName, ObjectId newId, ObjectId oldId);
}