<?php

namespace App\Services\Api;

use App\Services\PwApi\Ban\BanService;
use App\Services\PwApi\Chat\ChatService;
use App\Services\PwApi\Clone\CloneService;
use App\Services\PwApi\Core\PwApiService;
use App\Services\PwApi\Faction\FactionService;
use App\Services\PwApi\Item\ItemService;
use App\Services\PwApi\Money\MoneyService;
use App\Services\PwApi\Role\RoleService;
use App\Services\PwApi\Role\StatusService;
use App\Services\PwApi\Server\ServerService;
use App\Services\PwApi\Territory\TerritoryService;

class API
{
    public $userId;

    public $data = [];

    public $gamed;

    protected PwApiService $core;

    protected RoleService $roleService;

    protected StatusService $statusService;

    protected ItemService $itemService;

    protected ChatService $chatService;

    protected FactionService $factionService;

    protected ServerService $serverService;

    protected CloneService $cloneService;

    protected MoneyService $moneyService;

    protected TerritoryService $territoryService;

    protected BanService $banService;

    protected array $services = [];

    public function __construct($userId = null)
    {
        $this->userId = $userId;

        $this->core = new PwApiService;
        $this->online = $this->core->online;
        $this->data = $this->core->data;
        $this->gamed = $this->core->gamed;
        $this->roleService = new RoleService;
        $this->statusService = new StatusService;
        $this->itemService = new ItemService;
        $this->chatService = new ChatService;
        $this->factionService = new FactionService;
        $this->serverService = new ServerService;
        $this->cloneService = new CloneService;
        $this->moneyService = new MoneyService;
        $this->territoryService = new TerritoryService;
        $this->banService = new BanService;

        $this->services = [
            'core' => $this->core,
            'roleService' => $this->roleService,
            'statusService' => $this->statusService,
            'itemService' => $this->itemService,
            'chatService' => $this->chatService,
            'factionService' => $this->factionService,
            'serverService' => $this->serverService,
            'cloneService' => $this->cloneService,
            'moneyService' => $this->moneyService,
            'territoryService' => $this->territoryService,
            'banService' => $this->banService,
        ];
    }

    /**
     * Lazy-resolve properties that are not directly set on this facade.
     */
    public function __get(string $name)
    {
        return $this->$name ?? null;
    }

    public function __call($method, $parameters)
    {
        foreach ($this->services as $service) {
            if (method_exists($service, $method)) {
                return $service->$method(...$parameters);
            }
        }

        throw new \BadMethodCallException("Method {$method} does not exist on API service.");
    }

    public static function __callStatic($method, $parameters)
    {
        $instance = new static;

        return $instance->$method(...$parameters);
    }
}
