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

namespace Workflow.Application.Features.WorkflowDefinitions.Commands;

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

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

        entity.IsEnabled = false;

        await db.SaveChangesAsync(cancellationToken);

        return Unit.Value;
    }
}
