<?php

declare(strict_types=1);

namespace ImovelZapAi\Automation\Application\Actions;

use ImovelZapAi\Automation\Domain\Enums\WorkflowStatus;
use ImovelZapAi\Automation\Domain\Exceptions\AutomationException;
use ImovelZapAi\Automation\Infrastructure\Persistence\Eloquent\AutomationWorkflowModel;

final class PauseWorkflowAction
{
    public function execute(string $tenantId, string $workflowId): AutomationWorkflowModel
    {
        $workflow = AutomationWorkflowModel::query()
            ->where('tenant_id', $tenantId)
            ->where('id', $workflowId)
            ->first();

        if ($workflow === null) {
            throw AutomationException::notFound('Automação');
        }

        $workflow->forceFill(['status' => WorkflowStatus::Paused])->save();

        return $workflow->fresh();
    }
}
