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

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

public sealed class GetIssueByIdQueryHandler : IRequestHandler<GetIssueByIdQuery, IssueDto?>
{
    private readonly IIssueRepository _issueRepository;

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

    public async Task<IssueDto?> Handle(GetIssueByIdQuery request, CancellationToken cancellationToken)
    {
        var issue = await _issueRepository.GetByIdAsync(request.IssueId, cancellationToken);

        if (issue == null)
            return null;

        return 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
        };
    }
}
