<?php
namespace boru\ai\Tests\Provider\Gemini;

use boru\ai\Asset\Asset;
use boru\ai\Provider\Gemini\GeminiAssetProvider;
use boru\ai\Tests\Fakes\FakeGeminiClient;
use PHPUnit\Framework\TestCase;

class GeminiAssetProviderTest extends TestCase
{
    public function testEnsureBindingUploadsAssetAndReturnsGeminiBinding()
    {
        $tmp = tempnam(sys_get_temp_dir(), 'gemini_asset_');
        file_put_contents($tmp, 'hello gemini file');

        $client = new FakeGeminiClient(array(
            array(
                'file' => array(
                    'name' => 'files/abc123',
                    'uri' => 'https://generativelanguage.googleapis.com/v1beta/files/abc123',
                    'mimeType' => 'text/plain',
                    'displayName' => 'notes.txt',
                    'state' => 'READY',
                    'sizeBytes' => 17,
                ),
            ),
        ));

        $provider = new GeminiAssetProvider($client);
        $asset = new Asset();
        $asset->id('asset_1')
            ->kind('file')
            ->path($tmp)
            ->filename('notes.txt')
            ->mimeType('text/plain');

        $binding = $provider->ensureBinding($asset, 'user_data');

        $this->assertSame('uploadFile', $client->requests[0]['method']);
        $this->assertSame($tmp, $client->requests[0]['path']);
        $this->assertSame('notes.txt', $client->requests[0]['options']['display_name']);
        $this->assertSame('gemini', $binding->provider());
        $this->assertSame('asset_1', $binding->assetId());
        $this->assertSame('files/abc123', $binding->providerAssetId());
        $this->assertSame('https://generativelanguage.googleapis.com/v1beta/files/abc123', $binding->providerUri());
        $this->assertSame('text/plain', $binding->mimeType());
        $this->assertSame('ready', $binding->status());

        @unlink($tmp);
    }

    public function testEnsureBindingUploadsOfficeMimeTypeForSpreadsheetFiles()
    {
        $tmp = tempnam(sys_get_temp_dir(), 'gemini_asset_');
        file_put_contents($tmp, 'xlsx payload');

        $client = new FakeGeminiClient(array(
            array(
                'file' => array(
                    'name' => 'files/xlsx123',
                    'uri' => 'https://generativelanguage.googleapis.com/v1beta/files/xlsx123',
                    'mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    'displayName' => 'sheet.xlsx',
                    'state' => 'READY',
                    'sizeBytes' => 12,
                ),
            ),
        ));

        $provider = new GeminiAssetProvider($client);
        $asset = new Asset();
        $asset->id('asset_xlsx')
            ->kind('file')
            ->path($tmp)
            ->filename('sheet.xlsx')
            ->mimeType(\boru\ai\Asset\MimeTypeResolver::detect('sheet.xlsx'));

        $binding = $provider->ensureBinding($asset, 'user_data');

        $this->assertSame('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $client->requests[0]['options']['mime_type']);
        $this->assertSame('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $binding->mimeType());

        @unlink($tmp);
    }
}
