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

namespace Workflow.Application.Features.WorkflowDefinitions.Commands;

public record DeleteWorkflowDefinitionCommand(Guid Id) : IRequest<Unit>;

public class DeleteWorkflowDefinitionCommandHandler(WorkflowDbContext db)
    : IRequestHandler<DeleteWorkflowDefinitionCommand, Unit>
{
    public async Task<Unit> Handle(DeleteWorkflowDefinitionCommand request, CancellationToken cancellationToken)
    {
        var entity = await db.WorkflowDefinitions.FindAsync([request.Id], cancellationToken)
            ?? throw new NotFoundException($"Workflow definition '{request.Id}' not found.");

        entity.IsDeleted = true;
        await db.SaveChangesAsync(cancellationToken);

        return Unit.Value;
    }
}
