using MediatR;
using ColaFlow.Modules.ProjectManagement.Application.DTOs;
using ColaFlow.Modules.ProjectManagement.Application.Common.Interfaces;
using ColaFlow.Modules.ProjectManagement.Domain.Repositories;
using ColaFlow.Modules.ProjectManagement.Domain.ValueObjects;
using ColaFlow.Modules.ProjectManagement.Domain.Exceptions;
using ColaFlow.Modules.ProjectManagement.Domain.Aggregates.ProjectAggregate;

namespace ColaFlow.Modules.ProjectManagement.Application.Commands.AssignTask;

/// <summary>
/// Handler for AssignTaskCommand
/// </summary>
public sealed class AssignTaskCommandHandler(
    IProjectRepository projectRepository,
    IUnitOfWork unitOfWork)
    : IRequestHandler<AssignTaskCommand, TaskDto>
{
    private readonly IProjectRepository _projectRepository = projectRepository ?? throw new ArgumentNullException(nameof(projectRepository));
    private readonly IUnitOfWork _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));

    public async Task<TaskDto> Handle(AssignTaskCommand request, CancellationToken cancellationToken)
    {
        // Get the project containing the task (Global Query Filter ensures tenant isolation)
        var taskId = TaskId.From(request.TaskId);
        var project = await _projectRepository.GetProjectWithTaskAsync(taskId, cancellationToken);

        if (project == null)
            throw new NotFoundException("Task", request.TaskId);

        // Find the task within the project aggregate
        WorkTask? task = null;
        foreach (var epic in project.Epics)
        {
            foreach (var story in epic.Stories)
            {
                task = story.Tasks.FirstOrDefault(t => t.Id.Value == request.TaskId);
                if (task != null)
                    break;
            }
            if (task != null)
                break;
        }

        if (task == null)
            throw new NotFoundException("Task", request.TaskId);

        // Assign task
        if (request.AssigneeId.HasValue)
        {
            var assigneeId = UserId.From(request.AssigneeId.Value);
            task.AssignTo(assigneeId);
        }
        else
        {
            // Unassign by setting to null - need to add this method to WorkTask
            task.AssignTo(null!);
        }

        // Update project
        _projectRepository.Update(project);
        await _unitOfWork.SaveChangesAsync(cancellationToken);

        // Map to DTO
        return new TaskDto
        {
            Id = task.Id.Value,
            Title = task.Title,
            Description = task.Description,
            StoryId = task.StoryId.Value,
            Status = task.Status.Name,
            Priority = task.Priority.Name,
            AssigneeId = task.AssigneeId?.Value,
            EstimatedHours = task.EstimatedHours,
            ActualHours = task.ActualHours,
            CreatedBy = task.CreatedBy.Value,
            CreatedAt = task.CreatedAt,
            UpdatedAt = task.UpdatedAt
        };
    }
}
