<?php

use PHPUnit\Framework\TestCase;
use boru\boruai\Openai\Models\Content;
use boru\boruai\Openai\OpenAIChat;

class ContentTest extends TestCase
{
    /** @var string[] */
    private array $tempFiles = [];

    protected function tearDown(): void
    {
        foreach ($this->tempFiles as $file) {
            if (file_exists($file)) {
                unlink($file);
            }
        }

        $this->tempFiles = [];
    }

    public function testAddFileUsesKnownSpreadsheetMimeTypeForLocalFiles(): void
    {
        $path = $this->makeTempFile('xlsx', 'spreadsheet bytes');

        $content = new Content();
        $content->addFile($path);

        $body = $content->toArray();

        $this->assertCount(1, $body);
        $this->assertSame('file', $body[0]['type']);
        $this->assertSame(basename($path), $body[0]['file']['filename']);
        $this->assertStringStartsWith(
            'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,',
            $body[0]['file']['file_data']
        );

        $encoded = substr($body[0]['file']['file_data'], strpos($body[0]['file']['file_data'], ',') + 1);
        $this->assertSame('spreadsheet bytes', base64_decode($encoded));
    }

    public function testAddFileWithFileIdKeepsNormalFileIdPayload(): void
    {
        $content = new Content();
        $content->addFile('file-123');

        $this->assertSame(
            [
                [
                    'type' => 'file',
                    'file' => [
                        'file_id' => 'file-123',
                    ],
                ],
            ],
            $content->toArray()
        );
    }

    public function testOpenAIChatAddMessageKeepsPlainTextUsageWorking(): void
    {
        $chat = new OpenAIChat();
        $chat->addMessage('user', 'Hello, how are you?');

        $messages = $chat->messages()->toArray();

        $this->assertCount(1, $messages);
        $this->assertSame('user', $messages[0]['role']);
        $this->assertSame('Hello, how are you?', $messages[0]['content']);
    }

    public function testOpenAIChatAddMessageWithFileStillBuildsMixedContentPayload(): void
    {
        $path = $this->makeTempFile('xlsx', 'spreadsheet bytes');

        $chat = new OpenAIChat();
        $chat->addMessage('user', 'Please review this spreadsheet', $path);

        $messages = $chat->messages()->toArray();

        $this->assertCount(1, $messages);
        $this->assertSame('user', $messages[0]['role']);
        $this->assertIsArray($messages[0]['content']);
        $this->assertCount(2, $messages[0]['content']);
        $this->assertSame('file', $messages[0]['content'][0]['type']);
        $this->assertStringStartsWith(
            'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,',
            $messages[0]['content'][0]['file']['file_data']
        );
        $this->assertSame(
            [
                'type' => 'text',
                'text' => 'Please review this spreadsheet',
            ],
            $messages[0]['content'][1]
        );
    }

    private function makeTempFile(string $extension, string $contents): string
    {
        $path = sys_get_temp_dir() . '/boruai-' . uniqid('', true) . '.' . $extension;
        file_put_contents($path, $contents);
        $this->tempFiles[] = $path;

        return $path;
    }
}
