<?php

namespace Tests\Feature;

use App\Agent\MarketingAgent;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Ai\Approvals\PendingApproval;
use Laravel\Ai\Responses\AgentResponse;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Tests\TestCase;

class ChatStreamingTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->user = User::factory()->create();
        $this->actingAs($this->user);

        MarketingAgent::fake(['Hello from stream!']);
    }

    public function test_send_returns_streamed_response_when_accepting_event_stream(): void
    {
        $response = $this->post(
            route('chat.send'),
            ['message' => 'Hello there'],
            ['Accept' => 'text/event-stream']
        );

        $response->assertOk();
        $this->assertInstanceOf(StreamedResponse::class, $response->baseResponse);
        $this->assertStringContainsString('text/event-stream', $response->headers->get('Content-Type'));

        $bufferLevel = ob_get_level();
        $content = $response->streamedContent();

        $this->assertSame($bufferLevel, ob_get_level());
        $this->assertStringContainsString('data: ', $content);
        $this->assertStringContainsString('"type":"delta"', $content);
        $this->assertStringContainsString('"delta":"Hello"', $content);
        $this->assertStringContainsString('"type":"done"', $content);
        $this->assertStringContainsString('[DONE]', $content);
    }

    public function test_send_returns_json_response_when_accepting_json(): void
    {
        $response = $this->postJson(route('chat.send'), [
            'message' => 'Hello there',
        ]);

        $response->assertOk()
            ->assertJsonStructure(['conversation', 'reply', 'replyHtml', 'choices']);
    }

    public function test_streamed_provider_failure_preserves_output_buffers_and_finishes_the_stream(): void
    {
        MarketingAgent::fake(fn () => throw new RuntimeException('Invalid provider credentials.'));

        $response = $this->post(
            route('chat.send'),
            ['message' => 'Hello there'],
            ['Accept' => 'text/event-stream'],
        );

        $response->assertOk();
        $bufferLevel = ob_get_level();
        $content = $response->streamedContent();

        $this->assertSame($bufferLevel, ob_get_level());
        $this->assertStringContainsString('"type":"error"', $content);
        $this->assertStringContainsString('"type":"done"', $content);
        $this->assertStringContainsString('[DONE]', $content);
    }

    public function test_send_does_not_stream_for_wildcard_or_missing_accept_header(): void
    {
        // Accept: */* must return JSON, not SSE
        $wildcard = $this->post(
            route('chat.send'),
            ['message' => 'Hello there'],
            ['Accept' => '*/*']
        );

        $wildcard->assertOk();
        $this->assertStringNotContainsString('text/event-stream', (string) $wildcard->headers->get('Content-Type'));
        $wildcard->assertJsonStructure(['conversation', 'reply']);

        // Missing Accept header must return JSON, not SSE
        $missing = $this->post(
            route('chat.send'),
            ['message' => 'Hello there'],
            []
        );

        $missing->assertOk();
        $this->assertStringNotContainsString('text/event-stream', (string) $missing->headers->get('Content-Type'));
        $missing->assertJsonStructure(['conversation', 'reply']);
    }

    public function test_send_streams_when_stream_boolean_parameter_is_passed(): void
    {
        $response = $this->post(
            route('chat.send'),
            ['message' => 'Hello there', 'stream' => '1'],
            ['Accept' => 'application/json']
        );

        $response->assertOk();
        $this->assertInstanceOf(StreamedResponse::class, $response->baseResponse);
    }

    /**
     * A streamed run that pauses for approval says so.
     *
     * The streamed path read a protected property and always got null, so
     * "Start campaign" paused for approval while the reply said everything
     * matched what was live and no approval was offered.
     */
    public function test_a_streamed_turn_paused_for_approval_offers_the_approval(): void
    {
        MarketingAgent::fake([AgentResponse::fakeWithPendingApprovals([
            new PendingApproval('call_start', 'campaign__activate_campaign', [], 'Start the campaign.'),
        ])]);

        $content = $this->post(
            route('chat.send'),
            ['message' => 'Yes, start the campaign.'],
            ['Accept' => 'text/event-stream'],
        )->streamedContent();

        $done = collect(explode("\n\n", $content))
            ->map(fn (string $line): mixed => json_decode(substr($line, 6), true))
            ->firstWhere('type', 'done');

        $this->assertSame('call_start', $done['awaiting_approval'][0]['id'] ?? null);
        $this->assertStringContainsString('Waiting on your approval', $done['reply'] ?? '');
    }
}
