using MediatR;
using Workflow.Domain.Enums;
using Workflow.Domain.Exceptions;
using Workflow.Infrastructure.Persistence;

namespace Workflow.Application.Features.WorkflowTasks.Commands;

public record UrgeTaskCommand(
    Guid TaskId,
    Guid UserId
) : IRequest<Unit>;

public class UrgeTaskCommandHandler(WorkflowDbContext db)
    : IRequestHandler<UrgeTaskCommand, Unit>
{
    public async Task<Unit> Handle(UrgeTaskCommand request, CancellationToken cancellationToken)
    {
        var task = await db.WorkflowTasks.FindAsync([request.TaskId], cancellationToken)
            ?? throw new NotFoundException($"Task '{request.TaskId}' not found.");

        if (task.Status != TaskStatus.Pending)
        {
            throw new BusinessException("Only pending tasks can be urged.");
        }

        // Urge logic: in a real system this would send a notification
        // For now, we validate the task is pending and return success
        // Notification/email sending would be handled by a domain event or outbox pattern

        await db.SaveChangesAsync(cancellationToken);

        return Unit.Value;
    }
}
