<?php

namespace App\Services;

use App\Services\Contracts\SmmPlatformInterface;
use InvalidArgumentException;

class SmmService
{
    protected SmmPlatformInterface $platform;

    public function __construct(string $platform)
    {
        $this->platform = $this->resolvePlatform($platform);
    }

    protected function resolvePlatform(string $platform): SmmPlatformInterface
    {
        $className = $this->getPlatformClassName($platform);


        if (!class_exists($className)) {
            throw new InvalidArgumentException("Platform [{$platform}] not supported");
        }

        $instance = new $className();

        if (!$instance instanceof SmmPlatformInterface) {
            throw new \RuntimeException("Platform [{$platform}] must implement SmmPlatformInterface");
        }

        return $instance;
    }

    protected function getPlatformClassName(string $platform): string {
        $platform = ucfirst(strtolower($platform));
        return "App\\Services\\Smm\\{$platform}Service";
    }

    // 代理调用平台方法
    public function __call(string $method, array $arguments) {
        if (method_exists($this->platform, $method)) {
            return $this->platform->{$method}(...$arguments);
        }

        throw new \BadMethodCallException("Method [{$method}] not found on platform");
    }
}
