using MediatR;
using ColaFlow.Modules.IssueManagement.Application.DTOs;
using ColaFlow.Modules.IssueManagement.Domain.Repositories;

namespace ColaFlow.Modules.IssueManagement.Application.Queries.ListIssuesByStatus;

public sealed class ListIssuesByStatusQueryHandler : IRequestHandler<ListIssuesByStatusQuery, List<IssueDto>>
{
    private readonly IIssueRepository _issueRepository;

    public ListIssuesByStatusQueryHandler(IIssueRepository issueRepository)
    {
        _issueRepository = issueRepository;
    }

    public async Task<List<IssueDto>> Handle(ListIssuesByStatusQuery request, CancellationToken cancellationToken)
    {
        var issues = await _issueRepository.GetByProjectIdAndStatusAsync(
            request.ProjectId,
            request.Status,
            cancellationToken);

        return issues.Select(issue => new IssueDto
        {
            Id = issue.Id,
            ProjectId = issue.ProjectId,
            TenantId = issue.TenantId,
            Title = issue.Title,
            Description = issue.Description,
            Type = issue.Type.ToString(),
            Status = issue.Status.ToString(),
            Priority = issue.Priority.ToString(),
            AssigneeId = issue.AssigneeId,
            ReporterId = issue.ReporterId,
            CreatedAt = issue.CreatedAt,
            UpdatedAt = issue.UpdatedAt
        }).ToList();
    }
}
