<?php

namespace Tests\Feature;

use App\Agent\Tools\Campaign\StartCampaign;
use App\Agent\Workspace;
use App\Models\Campaign;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Laravel\Ai\Tools\Request;
use Tests\TestCase;

/**
 * The campaign in focus is the one the questions are about.
 *
 * Seen on dev on 23 Sep: starting on Meta and Google in one message put Meta in
 * focus, so choose_objective correctly offered Meta's five objectives, and the
 * panel beside it showed the Google campaign with Google's fields. The buyer
 * read "Needs a pixel" and "reactions, comments and shares" against a campaign
 * headed google.
 *
 * Neither half was wrong on its own. StartCampaign focuses the first platform
 * deliberately, with focus: $created === [], because the first is what the user
 * was talking about. Workspace::flush() then replayed every buffered artifact
 * through produced() without that flag, which defaults to true, so each one
 * re-focused as it was written and the last platform won.
 *
 * Only on the first message of a chat, because that is the only time artifacts
 * are buffered rather than written straight through.
 */
class FocusSurvivesTheFirstTurnTest extends TestCase
{
    use RefreshDatabase;

    public function test_the_first_platform_stays_in_focus_after_the_conversation_exists(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $workspace = app(Workspace::class);
        $workspace->ownedBy($user);

        // No conversation yet, which is what makes the artifacts buffer.
        app(StartCampaign::class)->handle(new Request(['platforms' => ['meta', 'google']]));

        $this->assertSame(
            'meta',
            $workspace->focus()?->platform,
            'The first platform should be in focus before the conversation exists.',
        );

        // The turn ends and the controller binds the workspace, flushing.
        $workspace->bindTo((string) Str::uuid());

        $this->assertSame(
            'meta',
            $workspace->focus()?->platform,
            'Flushing re-focused each campaign in turn, so the last platform stole focus.',
        );
    }

    /**
     * Both campaigns still exist and are both in the conversation.
     *
     * The fix must not stop the second platform being recorded, only stop it
     * taking focus.
     */
    public function test_every_platform_is_still_recorded(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $workspace = app(Workspace::class);
        $workspace->ownedBy($user);

        app(StartCampaign::class)->handle(new Request(['platforms' => ['meta', 'google']]));
        $workspace->bindTo((string) Str::uuid());

        $this->assertEqualsCanonicalizing(
            ['meta', 'google'],
            Campaign::query()->pluck('platform')->all(),
        );

        $this->assertCount(
            2,
            $workspace->artifacts(Campaign::class),
            'Both campaigns should be linked to the conversation.',
        );
    }
}
