using FluentResults;
using MediatR;
using NexusReader.Application.Abstractions.Services;
using NexusReader.Application.DTOs.AI;

namespace NexusReader.Application.Queries.Library;

public record SearchLibrarySemanticallyQuery(string QueryText, string TenantId, int Limit = 5) 
    : IRequest<Result<List<SemanticSearchResultDto>>>;

public class SearchLibrarySemanticallyQueryHandler : IRequestHandler<SearchLibrarySemanticallyQuery, Result<List<SemanticSearchResultDto>>>
{
    private readonly IKnowledgeService _knowledgeService;

    public SearchLibrarySemanticallyQueryHandler(IKnowledgeService knowledgeService)
    {
        _knowledgeService = knowledgeService;
    }

    public async Task<Result<List<SemanticSearchResultDto>>> Handle(SearchLibrarySemanticallyQuery request, CancellationToken cancellationToken)
    {
        if (string.IsNullOrWhiteSpace(request.QueryText))
        {
            return Result.Fail("Query text cannot be empty.");
        }

        return await _knowledgeService.SearchLibrarySemanticallyAsync(
            request.QueryText, 
            request.TenantId, 
            request.Limit, 
            cancellationToken);
    }
}
