<?php

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

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    return json_encode(['error' => 'Method not allowed']);
}

if (!isset($_FILES['file'])) {
    http_response_code(400);
    return json_encode(['error' => 'No file uploaded']);
}

$uploadedImage = uploadImage($_FILES['file']);

if ($uploadedImage) {
    return json_encode(['location' => "/$uploadedImage"]);
} else {
    http_response_code(400);
    return json_encode(['error' => 'Failed to upload image. Please check file format and size.']);
}