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

namespace ColaFlow.Modules.ProjectManagement.Application.Queries.GetActiveSprints;

/// <summary>
/// Handler for GetActiveSprintsQuery
/// </summary>
public sealed class GetActiveSprintsQueryHandler(IProjectRepository projectRepository)
    : IRequestHandler<GetActiveSprintsQuery, IReadOnlyList<SprintDto>>
{
    private readonly IProjectRepository _projectRepository = projectRepository ?? throw new ArgumentNullException(nameof(projectRepository));

    public async Task<IReadOnlyList<SprintDto>> Handle(GetActiveSprintsQuery request, CancellationToken cancellationToken)
    {
        var sprints = await _projectRepository.GetActiveSprintsAsync(cancellationToken);

        return sprints.Select(sprint => new SprintDto
        {
            Id = sprint.Id.Value,
            ProjectId = sprint.ProjectId.Value,
            ProjectName = string.Empty, // Could join with project if needed
            Name = sprint.Name,
            Goal = sprint.Goal,
            StartDate = sprint.StartDate,
            EndDate = sprint.EndDate,
            Status = sprint.Status.Name,
            TotalTasks = sprint.TaskIds.Count,
            CompletedTasks = 0, // TODO: Calculate from tasks
            TotalStoryPoints = 0, // TODO: Calculate from tasks
            RemainingStoryPoints = 0, // TODO: Calculate from tasks
            TaskIds = sprint.TaskIds.Select(t => t.Value).ToList(),
            CreatedAt = sprint.CreatedAt,
            CreatedBy = sprint.CreatedBy.Value,
            UpdatedAt = sprint.UpdatedAt
        }).ToList();
    }
}
