<?php
/**
 * Tom's Java Jive - Admin Image Upload Handler
 */
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/db.php';

header('Content-Type: application/json');

if (!AdminAuth::getUser()) {
    http_response_code(401);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['error' => 'No file received']);
    exit;
}

$result = handleImageUpload('image', __DIR__ . '/../uploads/products/', 'product_');

if (isset($result['success'])) {
    echo json_encode(['success' => true, 'url' => '/uploads/products/' . $result['filename']]);
} else {
    echo json_encode(['error' => $result['error']]);
}
