<?php

namespace App\Campaigns\Manager;

use App\Campaigns\Manager\Readers\GoogleStructureReader;
use App\Campaigns\Manager\Readers\LinkedInStructureReader;
use App\Campaigns\Manager\Readers\MetaStructureReader;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;

/**
 * Which platforms the campaign manager can read, and the reader for each.
 *
 * Deliberately the platforms with a publisher in config/platforms.php. TikTok
 * and Taboola have rule classes but no API client that can read a campaign,
 * so they are refused by name rather than shown as empty.
 */
class StructureReaders
{
    /** @var array<string, class-string<StructureReader>> */
    private const array READERS = [
        'meta' => MetaStructureReader::class,
        'google' => GoogleStructureReader::class,
        'linkedin' => LinkedInStructureReader::class,
    ];

    public function __construct(private readonly Container $container) {}

    /** @return list<string> */
    public static function platforms(): array
    {
        return array_keys(self::READERS);
    }

    public static function supports(?string $platform): bool
    {
        return isset(self::READERS[(string) $platform]);
    }

    public function for(string $platform): StructureReader
    {
        if (! self::supports($platform)) {
            throw new InvalidArgumentException(sprintf('The campaign manager cannot read %s campaigns yet.', ucfirst($platform)));
        }

        return $this->container->make(self::READERS[$platform]);
    }
}
