using Heimdall.Application.DTOs; using Heimdall.Application.Notifications.Commands.MarkAllAsRead; using Heimdall.Application.Notifications.Commands.MarkAsRead; using Heimdall.Application.Notifications.Commands.UpdateConfig; using Heimdall.Application.Notifications.Queries.GetConfig; using Heimdall.Application.Notifications.Queries.GetNotifications; using Heimdall.Application.Notifications.Queries.GetUnreadCount; using Heimdall.Infrastructure.Security; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; using System.Security.Claims; namespace Heimdall.Web.Controllers; /// /// In-app notifications for users — list, mark read, configure notification preferences. /// [ApiController] [Route("api/notifications")] [EnableRateLimiting("Api")] [Authorize(AuthenticationSchemes = "JwtBearer,Cookies")] public class NotificationsController : ControllerBase { private readonly IMediator _mediator; public NotificationsController(IMediator mediator) => _mediator = mediator; private Guid GetUserId() => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); /// List notifications for the current user. [HttpGet] [ProducesResponseType(typeof(PagedResult), StatusCodes.Status200OK)] public async Task GetNotifications( [FromQuery] bool? isRead, [FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default) { var result = await _mediator.Send(new GetNotificationsQuery(GetUserId(), isRead, page, pageSize), ct); return Ok(result); } /// Get unread notification count. [HttpGet("unread-count")] [ProducesResponseType(typeof(UnreadCountDto), StatusCodes.Status200OK)] public async Task GetUnreadCount(CancellationToken ct = default) { var result = await _mediator.Send(new GetUnreadCountQuery(GetUserId()), ct); return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error }); } /// Mark a single notification as read. [HttpPost("{id:guid}/read")] [ProducesResponseType(typeof(MarkAsReadResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)] public async Task MarkAsRead(Guid id, CancellationToken ct = default) { var result = await _mediator.Send(new MarkAsReadCommand(id, GetUserId()), ct); return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 404, new { error = result.Error }); } /// Mark all notifications as read. [HttpPost("mark-all-read")] [ProducesResponseType(typeof(MarkAsReadResponse), StatusCodes.Status200OK)] public async Task MarkAllAsRead(CancellationToken ct = default) { var result = await _mediator.Send(new MarkAllAsReadCommand(GetUserId()), ct); return Ok(result.Data); } /// Get notification configuration for the current user. [HttpGet("config")] [ProducesResponseType(typeof(UserNotificationConfigDto), StatusCodes.Status200OK)] public async Task GetConfig(CancellationToken ct = default) { var result = await _mediator.Send(new GetNotificationConfigQuery(GetUserId()), ct); return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error }); } /// Update notification configuration for the current user. [HttpPut("config")] [ProducesResponseType(typeof(UserNotificationConfigDto), StatusCodes.Status200OK)] public async Task UpdateConfig([FromBody] UpdateNotificationConfigRequest request, CancellationToken ct = default) { var command = new UpdateNotificationConfigCommand( GetUserId(), request.EmailEnabled, request.EmailAddress, request.NtfyEnabled, request.NtfyUrl, request.Language, request.DailyDigest, request.WeeklyDigest, request.SubscribedEvents); var result = await _mediator.Send(command, ct); return result.IsSuccess ? Ok(result.Data) : StatusCode(result.StatusCode ?? 400, new { error = result.Error }); } }