namespace Health.Application.Notifications;

public sealed class InAppNotificationService(IInAppNotificationRepository notifications) : IInAppNotificationService
{
    private readonly IInAppNotificationRepository _notifications = notifications;

    public async Task<IReadOnlyList<InAppNotificationDto>> GetPendingAsync(Guid userId, CancellationToken ct)
    {
        var records = await _notifications.GetPendingAsync(userId, ct);
        return records.Select(ToDto).ToList();
    }

    public async Task<IReadOnlyList<InAppNotificationDto>> GetHistoryAsync(Guid userId, CancellationToken ct)
    {
        var records = await _notifications.GetHistoryAsync(userId, ct);
        return records.Select(ToDto).ToList();
    }

    public Task<bool> AcknowledgeAsync(Guid userId, Guid notificationId, CancellationToken ct) =>
        _notifications.AcknowledgeAsync(userId, notificationId, ct);

    public Task<int> MarkAllReadAsync(Guid userId, CancellationToken ct) =>
        _notifications.MarkAllReadAsync(userId, ct);

    public Task<bool> DeleteAsync(Guid userId, Guid notificationId, CancellationToken ct) =>
        _notifications.DeleteAsync(userId, notificationId, ct);

    private static InAppNotificationDto ToDto(InAppNotificationRecord record) => new(
        record.Id, record.Type, record.Title, record.Message, record.Severity,
        record.ActionType, record.ActionTargetId, record.IsRead, record.CreatedAt);
}
