<?php
namespace boru\ai\Tests\Asset;

use boru\ai\Asset\Asset;
use boru\ai\Asset\AssetProviderInterface;
use boru\ai\Asset\AssetResolver;
use boru\ai\Asset\ProviderAssetBinding;
use PHPUnit\Framework\TestCase;

class AssetArchitectureTest extends TestCase
{
    public function testCoreAssetIsProviderAgnostic()
    {
        $asset = new Asset();
        $asset->id('asset_1')
            ->kind('file')
            ->path('/tmp/report.pdf')
            ->filename('report.pdf')
            ->mimeType('application/pdf')
            ->sha256('abc123');

        $data = $asset->toArray();

        $this->assertSame('asset_1', $data['id']);
        $this->assertSame('application/pdf', $data['mime_type']);
        $this->assertArrayNotHasKey('provider_asset_id', $data);
    }

    public function testProviderBindingIsSeparateFromCoreAsset()
    {
        $binding = new ProviderAssetBinding();
        $binding->assetId('asset_1')
            ->provider('openai')
            ->providerAssetId('file-abc')
            ->purpose('vision')
            ->status('ready');

        $data = $binding->toArray();

        $this->assertSame('asset_1', $data['asset_id']);
        $this->assertSame('openai', $data['provider']);
        $this->assertSame('file-abc', $data['provider_asset_id']);
    }

    public function testAssetResolverReturnsProviderSpecificBinding()
    {
        $asset = new Asset();
        $asset->id('asset_1')->kind('image');

        $provider = new class implements AssetProviderInterface {
            public function providerName()
            {
                return 'gemini';
            }

            public function ensureBinding(Asset $asset, $purpose = null, ProviderAssetBinding $existingBinding = null)
            {
                if ($existingBinding) {
                    return $existingBinding;
                }

                $binding = new ProviderAssetBinding();
                $binding->assetId($asset->id());
                $binding->provider('gemini');
                $binding->providerAssetId('gemini-file-1');
                $binding->purpose($purpose);
                $binding->status('ready');
                return $binding;
            }
        };

        $resolver = new AssetResolver();
        $binding = $resolver->resolveBinding($provider, $asset, 'vision');

        $this->assertSame('asset_1', $binding->assetId());
        $this->assertSame('gemini', $binding->provider());
        $this->assertSame('gemini-file-1', $binding->providerAssetId());
        $this->assertSame('vision', $binding->purpose());
    }
}
