<?php
if (!defined('CMSB_VERSION')) { 
    define('CMSB_VERSION', true);
}

function getYelpBusinessReviews($businessId, $limit = 3) {
    $LOADSTRUCT = true;
    $settings = require __DIR__ . '/../data/settings.dat.php';
    $apiKey = $settings['yelp_APIKEY'];
    $apiUrl = $settings['yelp_URL'] . $businessId . '/reviews';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl . '?limit=' . $limit);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json'
    ]);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode === 200) {
        return json_decode($response, true);
    }
    return null;
}

function getYelpBusinessDetails($businessId) {
    $LOADSTRUCT = true;
    $settings = require __DIR__ . '/../data/settings.dat.php';
    $apiKey = $settings['yelp_APIKEY'];
    $apiUrl = $settings['yelp_URL'] . $businessId;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json'
    ]);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode === 200) {
        return json_decode($response, true);
    }
    return null;
}

function getYelpBusinessDataWithImages($businessId, $limit = 3) {
    // Get business details first
    $businessData = getYelpBusinessDetails($businessId);
    
    // Get reviews
    $reviewsData = getYelpBusinessReviews($businessId, $limit);
    
    // Combine the data
    $combinedData = [
        'business' => $businessData,
        'reviews' => $reviewsData['reviews'] ?? []
    ];
    
    return $combinedData;
}

function updateFoodieExperienceWithYelp($foodieExperienceData) {
    if (empty($foodieExperienceData['listingsNum'])) {
        return $foodieExperienceData;
    }
    
    $listing = mysql_get('listings', $foodieExperienceData['listingsNum']);
    if ($listing && !empty($listing['yelp_id'])) {
        $yelpData = getYelpBusinessData($listing['yelp_id']);
        if ($yelpData) {
            $foodieExperienceData['yelp_rating'] = $yelpData['rating'] ?? null;
            $foodieExperienceData['yelp_review_count'] = $yelpData['review_count'] ?? null;
            $foodieExperienceData['yelp_price'] = $yelpData['price'] ?? null;
        }
    }
    
    return $foodieExperienceData;
}

// function yelpSearchBusiness($name, $location = '', $latitude = null, $longitude = null) {
//     $settings = require __DIR__ . '/../data/settings.dat.php';
//     $apiKey = $settings['yelp_APIKEY'];
//     $url = 'https://api.yelp.com/v3/businesses/search?term=' . urlencode($name);

//     if ($latitude && $longitude) {
//         $url .= "&latitude=" . urlencode($latitude) . "&longitude=" . urlencode($longitude);
//     } elseif ($location) {
//         $url .= "&location=" . urlencode($location);
//     } else {
//         return null; // Location info required
//     }

//     $ch = curl_init();
//     curl_setopt($ch, CURLOPT_URL, $url);
//     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($ch, CURLOPT_HTTPHEADER, [
//         'Authorization: Bearer ' . $apiKey
//     ]);
//     $response = curl_exec($ch);
//     curl_close($ch);

//     $data = json_decode($response, true);
//     return $data['businesses'][0] ?? null; // Return first match or null
// }

// function yelpGetBusinessDetails($businessId) {
//     $settings = require __DIR__ . '/../data/settings.dat.php';
//     $apiKey = $settings['yelp_APIKEY'];
//     $url = $settings['yelp_URL'] . $businessId;

//     $ch = curl_init();
//     curl_setopt($ch, CURLOPT_URL, $url);
//     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($ch, CURLOPT_HTTPHEADER, [
//         'Authorization: Bearer ' . $apiKey
//     ]);
//     $response = curl_exec($ch);
//     curl_close($ch);

//     return json_decode($response, true);
// }
?>