<?php

namespace App\Libraries;

class NaverApiClient
{
    protected $baseUrl = 'https://test-b2b.land.naver.com';
    protected $charger = '';

    /**
     * [GET] 매물 정보 조회
     */
    public function getArticleInfo(string $articleNumber): ?array
    {
        $this->charger = 'admin';
        $url = "{$this->baseUrl}/kiso/center/verification-article/{$articleNumber}?charger={$this->charger}";

        return $this->request('GET', $url);
    }

    /**
     * [PUT] 매물 정보 수정
     * @param string $articleNumber 매물번호
     * @param array $updateData 수정할 데이터 (tradeType, price, space 등)
     */
    public function updateArticleInfo(string $articleNumber, array $updateData, string $charger = 'admin'): ?array
    {
        $this->charger = $charger;
        $url = "{$this->baseUrl}/kiso/center/verification-article/{$articleNumber}?charger={$this->charger}";
        
        return $this->request('PUT', $url, $updateData);
    }

    /**
     * CURL 공통 실행 함수
     */
    private function request(string $method, string $url, ?array $data = null): ?array
    {
        /**
         * curl --location 'https://test-b2b.land.naver.com/kiso/center/verification-article/2500000001?charger=admin' \
--header 'X-Naver-Client-Id: yqBbvQZ123_hjH3b3Df9' \
         */

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        if ($method === 'PUT') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            if ($data) {
                $payload = json_encode($data);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($payload),
                    'X-Naver-Client-Id: yqBbvQZ123_hjH3b3Df9'
                ]);
            }
        } elseif ($method === 'GET') {
            curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'X-Naver-Client-Id: yqBbvQZ123_hjH3b3Df9'
            ]);
        } elseif ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            if ($data) {
                $payload = json_encode($data);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($payload),
                    'X-Naver-Client-Id: yqBbvQZ123_hjH3b3Df9'
                ]);
            }
        }

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        // 결과 로그 기록 (성공/실패 모두 기록하여 추적 가능하게 함)
        if ($httpCode === 200 && $httpdCode === 202) {
            log_message('info', "[Naver API $method SUCCESS] URL: $url | Response: $response");
        } else {
            log_message('error', "[Naver API $method FAIL] URL: $url | Code: $httpCode | Response: $response");
            return null;
        }

        return json_decode($response, true);
    }
}