<?php

use App\Enums\PredictionSource;
use App\Enums\TrendDirection;
use App\Models\BrentPrice;
use App\Services\ApiLogger;
use App\Services\LlmPrediction\AnthropicPredictionProvider;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;

beforeEach(function (): void {
    Http::preventStrayRequests();
    config(['services.anthropic.api_key' => 'test-key']);
    $this->provider = new AnthropicPredictionProvider(new ApiLogger);
});

it('returns null when api key is not configured', function (): void {
    config(['services.anthropic.api_key' => null]);

    $prices = fakePrices(14);

    expect($this->provider->predict($prices))->toBeNull();
});

it('uses submit_prediction tool in the basic request', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::response([
            'stop_reason' => 'tool_use',
            'content' => [[
                'type' => 'tool_use',
                'name' => 'submit_prediction',
                'input' => ['direction' => 'rising', 'confidence' => 72, 'reasoning' => 'Prices rising.'],
            ]],
        ]),
    ]);

    // context request fails, falls back to basic
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([], 500)
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'rising', 'confidence' => 72, 'reasoning' => 'Prices rising.'],
                ]],
            ]),
    ]);

    $this->provider->predict(fakePrices(14));

    Http::assertSent(function ($request) {
        $tools = $request->data()['tools'] ?? [];

        return collect($tools)->contains(fn ($t) => $t['name'] === 'submit_prediction');
    });
});

it('returns a prediction with Llm source from basic tool use', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([], 500) // context fails
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'rising', 'confidence' => 72, 'reasoning' => 'Consistent upward trend.'],
                ]],
            ]),
    ]);

    $prediction = $this->provider->predict(fakePrices(14));

    expect($prediction->direction)->toBe(TrendDirection::Rising)
        ->and($prediction->source)->toBe(PredictionSource::Llm)
        ->and($prediction->confidence)->toBe(72)
        ->and($prediction->reasoning)->toBe('Consistent upward trend.');
});

it('caps confidence at 85', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([], 500)
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'falling', 'confidence' => 99, 'reasoning' => 'Very confident.'],
                ]],
            ]),
    ]);

    $prediction = $this->provider->predict(fakePrices(14));

    expect($prediction->confidence)->toBe(85);
});

it('returns null when tool_use block is missing from response', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([], 500)
            ->push([
                'stop_reason' => 'end_turn',
                'content' => [['type' => 'text', 'text' => 'Sorry, I cannot help.']],
            ]),
    ]);

    expect($this->provider->predict(fakePrices(14)))->toBeNull();
});

it('sends web_search tool during context prediction phase', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([
                'stop_reason' => 'end_turn',
                'content' => [['type' => 'text', 'text' => 'Searched and analysed.']],
            ])
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'flat', 'confidence' => 50, 'reasoning' => 'No clear trend.'],
                ]],
            ]),
    ]);

    $this->provider->predict(fakePrices(20));

    Http::assertSent(function ($request) {
        $tools = $request->data()['tools'] ?? [];

        return collect($tools)->contains(fn ($t) => ($t['type'] ?? '') === 'web_search_20250305');
    });
});

it('returns LlmWithContext source when context prediction succeeds', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([
                'stop_reason' => 'end_turn',
                'content' => [['type' => 'text', 'text' => 'Analysed news.']],
            ])
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'rising', 'confidence' => 70, 'reasoning' => 'OPEC+ cuts support prices.'],
                ]],
            ]),
    ]);

    $prediction = $this->provider->predict(fakePrices(20));

    expect($prediction->source)->toBe(PredictionSource::LlmWithContext)
        ->and($prediction->direction)->toBe(TrendDirection::Rising);
});

it('continues on pause_turn during web search phase', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([
                'stop_reason' => 'pause_turn',
                'content' => [['type' => 'server_tool_use', 'name' => 'web_search', 'input' => ['query' => 'Brent crude']]],
            ])
            ->push([
                'stop_reason' => 'end_turn',
                'content' => [['type' => 'text', 'text' => 'Done searching.']],
            ])
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'falling', 'confidence' => 60, 'reasoning' => 'Demand fears.'],
                ]],
            ]),
    ]);

    $prediction = $this->provider->predict(fakePrices(20));

    expect($prediction)->not->toBeNull()
        ->and($prediction->direction)->toBe(TrendDirection::Falling);

    Http::assertSentCount(3);
});

it('falls back to basic prediction when context phase fails', function (): void {
    Http::fake([
        'https://api.anthropic.com/*' => Http::sequence()
            ->push([], 500) // context search fails
            ->push([
                'stop_reason' => 'tool_use',
                'content' => [[
                    'type' => 'tool_use',
                    'name' => 'submit_prediction',
                    'input' => ['direction' => 'rising', 'confidence' => 65, 'reasoning' => 'Rising trend.'],
                ]],
            ]),
    ]);

    $prediction = $this->provider->predict(fakePrices(14));

    expect($prediction->source)->toBe(PredictionSource::Llm);
});

// --- helpers ---

function fakePrices(int $count): Collection
{
    return collect(range(1, $count))->map(fn (int $i) => new BrentPrice([
        'date' => now()->subDays($count - $i)->toDateString(),
        'price_usd' => 75.0 + $i,
    ]));
}
