<?php

namespace App\Campaigns\Publishing;

use App\Campaigns\Platforms\Platforms;
use Illuminate\Contracts\Container\Container;

/**
 * Which platforms this product can actually create a campaign on.
 *
 * Separate from {@see Platforms} on purpose. That one
 * answers "what does this platform accept", which is known for every platform
 * the product has rules for. This one answers "can we build one", which is a
 * much smaller list, and conflating them is how a campaign for a platform we
 * cannot publish to ended up being sent to the platform we can.
 *
 * Null rather than an exception for an unknown platform: the caller turns it
 * into a reason the user reads, and a missing publisher is an ordinary state of
 * the product rather than a bug.
 */
class Publishers
{
    public function __construct(private readonly Container $container) {}

    public function for(?string $platform): ?PlatformPublisher
    {
        $class = $platform === null
            ? null
            : config("platforms.publishers.{$platform}");

        return $class === null ? null : $this->container->make($class);
    }

    public function supports(?string $platform): bool
    {
        return $this->for($platform) !== null;
    }

    /** @return list<string> the platforms a campaign can actually be created on */
    public function keys(): array
    {
        return array_keys((array) config('platforms.publishers', []));
    }
}
