<?php

namespace App\Services\Platforms\Auth;

use App\Contracts\PlatformTokenRefresher;
use App\Models\PlatformConnection;
use InvalidArgumentException;

class TokenRefresherFactory
{
    /**
     * Resolve the token refresher for the given connection.
     */
    public function for(PlatformConnection $connection): PlatformTokenRefresher
    {
        return $this->forPlatform($connection->platform);
    }

    /**
     * Resolve the token refresher for the given platform string.
     */
    public function forPlatform(string $platform): PlatformTokenRefresher
    {
        return match (strtolower(trim($platform))) {
            'google' => app(GoogleTokenRefresher::class),
            'linkedin' => app(LinkedInTokenRefresher::class),
            'meta' => app(MetaTokenRefresher::class),
            default => throw new InvalidArgumentException(sprintf(
                'No token refresher is registered for platform [%s].',
                $platform,
            )),
        };
    }
}
