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

namespace Workflow.Application.Form.FormDefinition.Commands;

public record DeleteFormDefinitionCommand(Guid Id) : IRequest;

public class DeleteFormDefinitionCommandHandler(WorkflowDbContext db)
    : IRequestHandler<DeleteFormDefinitionCommand>
{
    public async Task Handle(DeleteFormDefinitionCommand request, CancellationToken cancellationToken)
    {
        var entity = await db.FormDefinitions
            .FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken)
            ?? throw new NotFoundException($"表单 {request.Id} 不存在");

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