<?php

namespace App\Services\Minimax;

use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;

class MinimaxClient
{
    public function __construct(private MinimaxAuthService $auth) {}

    /**
     * Build the base URL for the given account's API.
     */
    private function baseUrl(string $account): string
    {
        $url = rtrim(config("minimax.accounts.{$account}.url"), '/');
        $orgId = config("minimax.accounts.{$account}.organization_id");
        return "{$url}/api/api/orgs/{$orgId}";
    }

    /**
     * Make an authenticated GET request.
     */
    public function get(string $account, string $path, array $query = []): Response
    {
        $token = $this->auth->getToken($account);
        $url = $this->baseUrl($account) . '/' . ltrim($path, '/');

        $response = Http::withToken($token)
            ->acceptJson()
            ->get($url, $query);

        if ($response->status() === 401) {
            // Token might be stale — refresh once
            $this->auth->forgetToken($account);
            $token = $this->auth->getToken($account);
            $response = Http::withToken($token)
                ->acceptJson()
                ->get($url, $query);
        }

        return $response;
    }

    /**
     * Make an authenticated POST request with a JSON body.
     */
    public function post(string $account, string $path, array $data = []): Response
    {
        $token = $this->auth->getToken($account);
        $url = $this->baseUrl($account) . '/' . ltrim($path, '/');

        $response = Http::withToken($token)
            ->acceptJson()
            ->post($url, $data);

        if ($response->status() === 401) {
            $this->auth->forgetToken($account);
            $token = $this->auth->getToken($account);
            $response = Http::withToken($token)
                ->acceptJson()
                ->post($url, $data);
        }

        return $response;
    }

    /**
     * Make an authenticated PUT request with a JSON body.
     */
    public function put(string $account, string $path, array $data = []): Response
    {
        $token = $this->auth->getToken($account);
        $url = $this->baseUrl($account) . '/' . ltrim($path, '/');

        $response = Http::withToken($token)
            ->acceptJson()
            ->put($url, $data);

        if ($response->status() === 401) {
            $this->auth->forgetToken($account);
            $token = $this->auth->getToken($account);
            $response = Http::withToken($token)
                ->acceptJson()
                ->put($url, $data);
        }

        return $response;
    }

    /**
     * Make an authenticated DELETE request.
     */
    public function delete(string $account, string $path): Response
    {
        $token = $this->auth->getToken($account);
        $url = $this->baseUrl($account) . '/' . ltrim($path, '/');

        $response = Http::withToken($token)
            ->acceptJson()
            ->delete($url);

        if ($response->status() === 401) {
            $this->auth->forgetToken($account);
            $token = $this->auth->getToken($account);
            $response = Http::withToken($token)
                ->acceptJson()
                ->delete($url);
        }

        return $response;
    }
}
