<?php

use Datalumo\PhpSdk\DataObjects\StreamEvent;

it('creates a text delta event', function () {
    $event = StreamEvent::fromArray([
        'type' => 'text_delta',
        'delta' => 'Hello ',
        'message_id' => 'msg-1',
    ]);

    expect($event)
        ->type->toBe('text_delta')
        ->data->toBe('Hello ')
        ->isTextDelta()->toBeTrue()
        ->isCitation()->toBeFalse()
        ->isStreamEnd()->toBeFalse()
        ->isError()->toBeFalse();
});

it('creates a citation event', function () {
    $event = StreamEvent::fromArray([
        'type' => 'citation',
        'citation' => ['title' => 'Doc Title', 'url' => 'https://example.com'],
    ]);

    expect($event)
        ->isCitation()->toBeTrue()
        ->citation()->toBe(['title' => 'Doc Title', 'url' => 'https://example.com']);
});

it('creates a stream end event', function () {
    $event = StreamEvent::fromArray([
        'type' => 'stream_end',
        'reason' => 'stop',
        'usage' => ['prompt_tokens' => 100, 'completion_tokens' => 50],
    ]);

    expect($event)
        ->isStreamEnd()->toBeTrue()
        ->raw->toHaveKey('usage');
});

it('detects error events', function () {
    $event = StreamEvent::fromArray([
        'type' => 'error',
        'message' => 'Something went wrong',
    ]);

    expect($event)
        ->isError()->toBeTrue()
        ->data->toBe('Something went wrong');
});

it('preserves raw data', function () {
    $raw = ['type' => 'stream_start', 'provider' => 'openai', 'model' => 'gpt-4o-mini'];
    $event = StreamEvent::fromArray($raw);

    expect($event->raw)->toBe($raw);
});
