<?php

namespace App\Modules\Stock\ImportMapSources;

use App\Contracts\ImportMapSource;
use App\Exceptions\InvalidImportMapSourceException;
use App\Models\ImportMap;

class ImportMapSourceFactory
{
    public const DEFAULT_SOURCE_TYPE = 'file';

    private const SOURCES = [
        'http_json' => HttpJsonSource::class,
    ];

    public function for(ImportMap $map): ImportMapSource
    {
        $type = $map->data['source_type'] ?? self::DEFAULT_SOURCE_TYPE;

        if (!isset(self::SOURCES[$type])) {
            throw InvalidImportMapSourceException::forType($type);
        }

        return app(self::SOURCES[$type]);
    }

    /**
     * Source-type options for the admin UI dropdown.
     * Includes 'file' as a selectable option even though for NOW,
     * file import-map is resolved inline rather than via the contract.
     *
     * The dropdown needs both so users can switch maps between
     * file and HTTP API sources.
     *
     * @return array<int, array{value: string, label: string}>
     */
    public static function availableTypes(): array
    {
        return [
            ['value' => 'file', 'label' => 'Remote file (FTP / SFTP)'],
            ['value' => 'http_json', 'label' => 'HTTP API (JSON)'],
        ];
    }
}
