<?php

use App\Models\ApiLog;
use App\Services\ApiLogger;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->apiLogger = new ApiLogger;
});

it('logs a successful GET request', function (): void {
    Http::fake(['https://example.com/data' => Http::response(['ok' => true])]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/data', fn () => Http::get('https://example.com/data'));

    $log = ApiLog::first();
    expect($log)->not->toBeNull()
        ->and($log->service)->toBe('test_service')
        ->and($log->method)->toBe('GET')
        ->and($log->url)->toBe('https://example.com/data')
        ->and($log->status_code)->toBe(200)
        ->and($log->error)->toBeNull()
        ->and($log->duration_ms)->toBeGreaterThanOrEqual(0);
});

it('logs a failed request and re-throws the exception', function (): void {
    Http::fake(['https://example.com/fail' => fn () => throw new RuntimeException('connection refused')]);

    expect(fn () => $this->apiLogger->send(
        'test_service', 'GET', 'https://example.com/fail',
        fn () => Http::get('https://example.com/fail')
    ))->toThrow(RuntimeException::class, 'connection refused');

    $log = ApiLog::first();
    expect($log)->not->toBeNull()
        ->and($log->status_code)->toBeNull()
        ->and($log->error)->toBe('connection refused');
});

it('captures response body as error when status is 4xx/5xx', function (): void {
    Http::fake(['https://example.com/missing' => Http::response('Not Found', 404)]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/missing', fn () => Http::get('https://example.com/missing'));

    $log = ApiLog::first();
    expect($log->status_code)->toBe(404)
        ->and($log->error)->toBe('Not Found');
});

it('logs a POST request with correct method', function (): void {
    Http::fake(['https://example.com/token' => Http::response(['token' => 'abc'], 201)]);

    $this->apiLogger->send('test_service', 'POST', 'https://example.com/token', fn () => Http::post('https://example.com/token', ['key' => 'val']));

    expect(ApiLog::first()->method)->toBe('POST');
});

it('records duration in milliseconds', function (): void {
    Http::fake(['https://example.com/slow' => Http::response([])]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/slow', fn () => Http::get('https://example.com/slow'));

    expect(ApiLog::first()->duration_ms)->toBeInt()->toBeGreaterThanOrEqual(0);
});

it('upcases the method', function (): void {
    Http::fake(['https://example.com/*' => Http::response([])]);

    $this->apiLogger->send('test_service', 'get', 'https://example.com/x', fn () => Http::get('https://example.com/x'));

    expect(ApiLog::first()->method)->toBe('GET');
});

it('does NOT store response_body for successful 2xx responses', function (): void {
    Http::fake(['https://example.com/ok' => Http::response(['large' => str_repeat('x', 5000)])]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/ok', fn () => Http::get('https://example.com/ok'));

    expect(ApiLog::first()->response_body)->toBeNull();
});

it('stores response_body when status code is 4xx', function (): void {
    Http::fake(['https://example.com/bad' => Http::response('{"error":"bad request"}', 400)]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/bad', fn () => Http::get('https://example.com/bad'));

    expect(ApiLog::first()->response_body)->toBe('{"error":"bad request"}');
});

it('stores response_body when status code is 5xx', function (): void {
    Http::fake(['https://example.com/boom' => Http::response('Internal Server Error', 500)]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/boom', fn () => Http::get('https://example.com/boom'));

    expect(ApiLog::first()->response_body)->toBe('Internal Server Error');
});

it('truncates response_body at the 64KB cap', function (): void {
    $hugeBody = str_repeat('A', 80_000);
    Http::fake(['https://example.com/huge' => Http::response($hugeBody, 502)]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/huge', fn () => Http::get('https://example.com/huge'));

    $log = ApiLog::first();
    expect(strlen($log->response_body))->toBe(65_536);
});

it('captures response_body when an HTTP RequestException is thrown', function (): void {
    Http::fake(['https://example.com/throw' => Http::response('upstream details', 502)]);

    expect(fn () => $this->apiLogger->send(
        'test_service', 'GET', 'https://example.com/throw',
        fn () => Http::throw()->get('https://example.com/throw')
    ))->toThrow(RequestException::class);

    expect(ApiLog::first()->response_body)->toBe('upstream details');
});

it('captures Anthropic usage tokens from a successful response', function (): void {
    Http::fake(['https://api.anthropic.com/v1/messages' => Http::response([
        'content' => [],
        'usage' => [
            'input_tokens' => 1234,
            'output_tokens' => 56,
            'cache_creation_input_tokens' => 8000,
            'cache_read_input_tokens' => 12000,
        ],
    ])]);

    $this->apiLogger->send('anthropic', 'POST', 'https://api.anthropic.com/v1/messages',
        fn () => Http::post('https://api.anthropic.com/v1/messages'));

    $log = ApiLog::first();
    expect($log->input_tokens)->toBe(1234)
        ->and($log->output_tokens)->toBe(56)
        ->and($log->cache_write_tokens)->toBe(8000)
        ->and($log->cache_read_tokens)->toBe(12000);
});

it('captures rate-limit headers from any provider response', function (): void {
    Http::fake(['https://api.anthropic.com/v1/messages' => Http::response(
        ['content' => [], 'usage' => ['input_tokens' => 100, 'output_tokens' => 10]],
        200,
        [
            'anthropic-ratelimit-input-tokens-remaining' => '38000',
            'anthropic-ratelimit-input-tokens-reset' => '2026-05-14T12:41:00Z',
        ],
    )]);

    $this->apiLogger->send('anthropic', 'POST', 'https://api.anthropic.com/v1/messages',
        fn () => Http::post('https://api.anthropic.com/v1/messages'));

    $log = ApiLog::first();
    expect($log->ratelimit_remaining)->toBe(38000)
        ->and($log->ratelimit_reset_at?->toIso8601String())->toBe('2026-05-14T12:41:00+00:00');
});

it('leaves token columns null for services without usage data', function (): void {
    Http::fake(['https://example.com/x' => Http::response(['ok' => true])]);

    $this->apiLogger->send('test_service', 'GET', 'https://example.com/x',
        fn () => Http::get('https://example.com/x'));

    $log = ApiLog::first();
    expect($log->input_tokens)->toBeNull()
        ->and($log->output_tokens)->toBeNull()
        ->and($log->cache_read_tokens)->toBeNull()
        ->and($log->cache_write_tokens)->toBeNull()
        ->and($log->ratelimit_remaining)->toBeNull()
        ->and($log->ratelimit_reset_at)->toBeNull();
});
