<?php

namespace App\Models;

use Illuminate\Support\Facades\Http;

class CoupangLib
{
    const SUB_ID = 'pick202303';
    const BASE_URL = 'https://api-gateway.coupang.com';
    const BASE_PATH = '/v2/providers/affiliate_open_api/apis/openapi';

    protected function getAuthorization(string $path): string
    {
        date_default_timezone_set("GMT+0");
        $datetime = date("ymd") . 'T' . date("His") . 'Z';
        $message = $datetime . 'GET' . str_replace("?", "", self::BASE_PATH . $path);
        $signature = hash_hmac('sha256', $message, SECRET_KEY);

        return sprintf("CEA algorithm=HmacSHA256, access-key=%s, signed-date=%s, signature=%s", ACCESS_KEY, $datetime, $signature);
    }

    /**
     * 카테고리 별 Best 상품 조회
     */
    public function getBestCategories(int $categoryID): ?array
    {
        $path = sprintf('/v1/products/bestcategories/%s?limit=%d&subId=%s', $categoryID, 100, self::SUB_ID);
        $auth = $this->getAuthorization($path);

        $response = Http::withHeaders([
            'Authorization' => $auth,
            'Content-Type' => 'application/json',
            'charset' => 'UTF-8'
        ])->get(self::BASE_URL . self::BASE_PATH . $path);

        return $response->json();
    }

    /**
     * 카테고리 별 Best 상품 조회
     */
    public function getSearch(?string $keyword): ?array
    {
        $path = sprintf('/products/search?keyword=%s&limit=%d&srpLinkOnly=false&subId=%s', urlencode($keyword), 10, self::SUB_ID);
        $auth = $this->getAuthorization($path);

        $response = Http::withHeaders([
            'Authorization' => $auth,
            'Content-Type' => 'application/json',
            'charset' => 'UTF-8'
        ])->get(self::BASE_URL . self::BASE_PATH . $path);

        return $response->json();
    }

    /**
     * 쿠팡 추천 상품 조회
     */
    public function getAllCoupangPL(): ?array
    {
        $path = sprintf('/v1/products/coupangPL?limit=100&subId=%s', self::SUB_ID);
        $auth = $this->getAuthorization($path);

        $response = Http::withHeaders([
            'Authorization' => $auth,
            'Content-Type' => 'application/json',
            'charset' => 'UTF-8'
        ])->get(self::BASE_URL . self::BASE_PATH . $path);

        return $response->json();
    }

    /**
     * 쿠팡 추천 상품 조회
     */
    public function getCoupangPL(int $brandID): ?array
    {
        $path = sprintf('/v1/products/coupangPL/%s?limit=%d&subId=%s', $brandID, 100, self::SUB_ID);
        $auth = $this->getAuthorization($path);

        $response = Http::withHeaders([
            'Authorization' => $auth,
            'Content-Type' => 'application/json',
            'charset' => 'UTF-8'
        ])->get(self::BASE_URL . self::BASE_PATH . $path);

        return $response->json();
    }

    /**
     * 골드박스 상품 조회
     */
    public function getGoldBox(): ?array
    {
        $path = sprintf('/v1/products/goldbox?subId=%s', self::SUB_ID);
        $auth = $this->getAuthorization($path);

        $response = Http::withHeaders([
            'Authorization' => $auth,
            'Content-Type' => 'application/json',
            'charset' => 'UTF-8'
        ])->get(self::BASE_URL . self::BASE_PATH . $path);

        return $response->json();
    }
}
