<?php

namespace App\Agent\Tools\Campaign\Concerns;

use App\Models\Campaign;

/**
 * Shared plumbing for tools that edit the campaign currently in focus.
 *
 * These tools take no campaign id. Which campaign is meant is a property of the
 * conversation, not of the argument list, which is what makes "make it thirty a
 * day" resolvable in a chat holding three campaigns.
 */
trait ActsOnFocusedCampaign
{
    protected function focused(): ?Campaign
    {
        $focus = $this->workspace->focus();

        return $focus instanceof Campaign ? $focus : null;
    }

    protected function noCampaign(): string
    {
        return $this->json([
            'ok' => false,
            'error' => 'No campaign is in focus. Create one, or switch focus to an existing one, first.',
        ]);
    }

    /** @param array<string,mixed> $payload */
    protected function json(array $payload): string
    {
        return json_encode($payload, JSON_UNESCAPED_SLASHES);
    }
}
