<?php

namespace Tests\Feature;

use App\Agent\Instructions;
use App\Agent\Workspace;
use App\Models\Campaign;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * The launch flow leads; it does not refuse.
 *
 * The flow exists because the agent used to wait to be asked, which left the
 * buyer driving a sequence they were supposed to be led through. But the
 * instruction that fixed that read "do not ask for anything later in the list
 * first", and a model reading it has no way to tell a rule about what it
 * volunteers from a rule about what it is allowed to do when told.
 *
 * So somebody who says "make it fifty a day" three steps early gets told to
 * wait, which is the opposite of the point. Media buyers do not work down a
 * list, and somebody who has never bought media has no idea there is one.
 */
class TheFlowIsAnOrderToAskInTest extends TestCase
{
    use RefreshDatabase;

    private function prompt(): string
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $workspace = app(Workspace::class);
        $workspace->bindTo('conv-flexible-flow');
        $workspace->ownedBy($user);

        $campaign = Campaign::create([
            'user_id' => $user->id,
            'name' => 'Winter Sale',
            'platform' => 'meta',
            'status' => 'draft',
            'landing_url' => 'https://example.com/',
        ]);

        $workspace->produced($campaign);

        return app(Instructions::class)->build();
    }

    public function test_the_flow_is_described_as_an_order_to_ask_in(): void
    {
        $text = $this->prompt();

        $this->assertStringContainsString('It is an order to', $text);
        $this->assertStringContainsString('ask in, not an order to obey', $text);
    }

    /** A request off the sequence is served in the same turn, not deferred. */
    public function test_a_request_off_the_sequence_is_served_first(): void
    {
        $this->assertStringContainsString('do that first, in the', $this->prompt());
    }

    /** Settled is not frozen. */
    public function test_anything_settled_can_still_be_changed(): void
    {
        $text = $this->prompt();

        $this->assertStringContainsString('can be changed at any point', $text);
        $this->assertStringContainsString('has to wait for its turn', $text);
    }

    /**
     * The flow still leads, which is the half worth keeping.
     *
     * Loosening it into "do whatever is asked" would put the buyer back in
     * charge of a sequence they cannot see.
     */
    public function test_the_next_step_is_still_named_and_still_leads(): void
    {
        $text = $this->prompt();

        $this->assertStringContainsString('NEXT STEP', $text);
        $this->assertStringContainsString('unprompted', $text);
    }

    /** And the old absolute wording is gone, not merely joined by the new rule. */
    public function test_the_old_do_not_ask_first_wording_is_gone(): void
    {
        $this->assertStringNotContainsString('later in the list first', $this->prompt());
    }
}
