<?php

namespace Tests\Tenant\ImportMapSources;

use App\Exceptions\InvalidImportMapSourceException;
use App\Models\ImportMap;
use App\Modules\Stock\ImportMapSources\HttpJsonSource;
use App\Modules\Stock\ImportMapSources\ImportMapSourceFactory;
use Tests\TenantTestCase;

class ImportMapSourceFactoryTest extends TenantTestCase
{
    public function testResolvesHttpJsonSource(): void
    {
        $map = ImportMap::factory()->create(['data' => ['source_type' => 'http_json']]);

        $source = (new ImportMapSourceFactory())->for($map);

        $this->assertInstanceOf(HttpJsonSource::class, $source);
    }

    public function testThrowsOnFileSourceBecauseFilesAreHandledInline(): void
    {
        // File-based maps are handled inline by App\Modules\Stock\ImportMap
        // and should never reach the factory. If they do, the contract has
        // been bypassed somewhere — throw loudly so we notice.
        $map = ImportMap::factory()->create(['data' => ['source_type' => 'file']]);

        $this->expectException(InvalidImportMapSourceException::class);
        (new ImportMapSourceFactory())->for($map);
    }

    public function testThrowsForUnknownSourceType(): void
    {
        $map = ImportMap::factory()->create(['data' => ['source_type' => 'webhook_async']]);

        $this->expectException(InvalidImportMapSourceException::class);
        (new ImportMapSourceFactory())->for($map);
    }

    public function testAvailableTypesIsExposedForAdminUi(): void
    {
        $types = ImportMapSourceFactory::availableTypes();

        $values = array_column($types, 'value');
        $this->assertContains('file', $values);
        $this->assertContains('http_json', $values);
    }
}
