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

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

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

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

    public async Task<List<IssueDto>> Handle(ListIssuesQuery request, CancellationToken cancellationToken)
    {
        var issues = await _issueRepository.GetByProjectIdAsync(request.ProjectId, 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();
    }
}
