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

namespace Workflow.Application.Form.FormDefinition.Commands;

public record DisableFormDefinitionCommand(Guid Id) : IRequest;

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

        entity.Status = FormStatus.Disabled;
        await db.SaveChangesAsync(cancellationToken);
    }
}
