<?php
header('Content-Type: application/json; charset=utf-8');

$apiKey = 'K82905695088957';
$uploadDir = __DIR__ . '/uploads/';

if (!file_exists($uploadDir)) mkdir($uploadDir, 0777, true);

if (!isset($_FILES['receipt_image']) || $_FILES['receipt_image']['error'] !== UPLOAD_ERR_OK) {
    echo json_encode(['error' => 'Dosya yüklenemedi!']);
    exit;
}

$tmpFile = $_FILES['receipt_image']['tmp_name'];
$filename = time() . '_' . basename($_FILES['receipt_image']['name']);
$targetFile = $uploadDir . $filename;

if (!move_uploaded_file($tmpFile, $targetFile)) {
    echo json_encode(['error' => 'move_uploaded_file başarısız oldu!']);
    exit;
}

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => 'https://api.ocr.space/parse/image',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'apikey' => $apiKey,
        'language' => 'tur',
        'file' => new CURLFILE($targetFile),
    ],
]);
$response = curl_exec($curl);
curl_close($curl);

echo $response; // JSON olarak döndür