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

namespace Workflow.Application.Features.WorkflowInstances.Commands;

public record ResumeWorkflowInstanceCommand(Guid InstanceId) : IRequest<Unit>;

public class ResumeWorkflowInstanceCommandHandler(WorkflowDbContext db)
    : IRequestHandler<ResumeWorkflowInstanceCommand, Unit>
{
    public async Task<Unit> Handle(ResumeWorkflowInstanceCommand request, CancellationToken cancellationToken)
    {
        var instance = await db.WorkflowInstances.FindAsync([request.InstanceId], cancellationToken)
            ?? throw new NotFoundException($"Workflow instance '{request.InstanceId}' not found.");

        instance.Status = InstanceStatus.Running;

        await db.SaveChangesAsync(cancellationToken);

        return Unit.Value;
    }
}
