<?php
/*ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);*/
require 'vendor/autoload.php'; // Assuming you've installed the DeepSeek PHP client

use DeepSeek\DeepSeekClient;

// Initialize the client with your API key
//$client = new DeepSeekClient('sk-efead9fab1c746ba8cc194ef36449477');
$client = DeepSeekClient::build('sk-efead9fab1c746ba8cc194ef36449477');
// Path to your receipt image
//$imagePath = 'path/to/your/receipt.jpg';
if(isset($_GET['file_url'])){
    if($_GET['file_url'] == ""){
        echo "No file_url";exit;
    }
}
$filetext = file_get_contents($_GET['file_url']);

try {
    // Upload the image and ask if it's a valid receipt
    $response = $client->query(
                     "Please analyze this JSON from google place api and determine if it's a valid receipt. " .
                    "Check for key elements like merchant name, date, receipt number, " .
                    "total amount,receipt date , and location. Respond in JSON" .
                    "if it meets receipt criteria, 'isvalid' is 'true', or 'false' if it doesn't, followed by your analysis.
                    ".'The response should be in this format : { "isvalid":"true","merchant_name": "MYNEWS RETAIL SDN BHD", "receipt_no": "2206887232474", "amount": "193.10", "receipt_date": "2024-08-24", "location": "LOT NO.3, JLN TEKNOLOGI 3/1, TMN SAINS SELANGOR 1, SEKSYEN 3 PJU 5 KOTA DAMANSARA, 47810 PETALING JAYA, SELANGOR" }. The google place JSON:'.$filetext
                    
    )->run();
    
    // Process the response
    //echo "Receipt Validation Result:\n";
    //var_dump($response);
    $result = json_decode($response);
    if($result == false || $result->choices == NULL){
        var_dump($response);exit;
    }
    $finalResult =  $result->choices[0]->message->content;
    $finalResult = str_replace("```json","",$finalResult);
    $finalResult = str_replace("```","",$finalResult);
    echo $finalResult;
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>