<?php

namespace App\Traits;

use Illuminate\Http\JsonResponse;
use Inertia\Inertia;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Redirect;

trait Api
{
    protected function get($url, $data = [])
    {
        $token = Cookie::get('auth_token');
        // dd($data, $url, $token);

        if (!$token) {
            abort(401, 'Unauthorized');
        }


        $response = Http::withToken($token)
            ->asJson()
            ->acceptJson()
            ->timeout(60)->get($url, $data);
        // dd($response->object());
        return $response;

    }

    protected function post($url, $data = [])
    {
        $token = Cookie::get('auth_token');

        if (!$token) {

            // throw new \Exception("User token not found");
            abort(401, 'Unauthorized');
        }
        $response = Http::withToken($token)
            ->asJson()
            ->acceptJson()
            ->timeout(60)->post($url, $data);
        return $response;
    }

    protected function patch($url, $data = [])
    {
        $token = Cookie::get('auth_token');

        if (!$token) {
            // throw new \Exception("User token not found");
            abort(401, 'Unauthorized');
        }

        $response = Http::withToken($token)
            ->asJson()
            ->acceptJson()
            ->timeout(60)->patch($url, $data);
        return $response;
    }



    protected function remove($url, $data = [])
    {
        $token = Cookie::get('auth_token');

        if (!$token) {
            throw new \Exception("User token not found in session");
        }
        $response = Http::withToken($token)
            ->timeout(60)
            ->asJson()
            ->acceptJson()
            ->delete($url, $data);
        // dd($url, $data, $response);

        return $response;

    }

    protected function sendRequest(string $method, string $route, array $requestData = [])
    {
        $token = '262|fGgoJ2OTaIvDj58wwuZakXwy6q8yvXHb7wn3laZd|yarmy_codes';
        $response = Http::withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json',
                    'Authorization' => 'Bearer ' . $token,
                ])->$method(self::$baseUrl . $route, $requestData);

        if ($response->successful()) {
            return $response->json();
        } elseif ($response->status() === 401) {
            $this->handleUnauthorized();
        }

        throw new \Exception("Request failed: " . $response->body());
    }




    protected function signOut($url)
    {
        $token = Cookie::get('auth_token');
        $response = Http::withToken($token)->timeout(60)
            ->asJson()
            ->acceptJson()
            ->post($url);
        // Log::channel('kedebah_api')->info('LOGOUT RESPONSE', $response->json());

        return new JsonResponse($response->json(), $response->status());

    }
}

