using MediatR;
using System.Security.Claims;
using Web.Api.Common;
using Web.Api.Extensions;

namespace Web.Api.Endpoints.Forum.Post;

internal sealed class Search : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("api/forum/posts", async (
            ISender sender,
            ClaimsPrincipal user,
            CancellationToken ct,
            int? boardID = null,
            int? boardPrefixID = null,
            int? search = null,
            string? keyword = null,
            string? startAt = null,
            string? endAt = null,
            int? sort = null,
            bool? isNotice = null,
            bool? isSecret = null,
            bool? isReply = null,
            bool? isDeleted = null,
            int page = 1,
            ushort perPage = 20
        ) =>
        {
            var memberID = user.GetMemberID();

            var query = new Application.Features.Api.Forum.Post.Search.Query(
                boardID,
                boardPrefixID,
                search,
                keyword,
                startAt,
                endAt,
                sort,
                isNotice,
                isSecret,
                isReply,
                isDeleted,
                page,
                perPage,
                memberID
            );

            return ApiResponse.Ok(await sender.Send(query, ct));
        })
        .WithTags("Forum")
        .AllowAnonymous();
    }
}
