<?php
session_start();
header('Content-Type: application/json');

require_once __DIR__ . '/../../classes/DB.php';

/* ADMIN CHECK */
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    echo json_encode(["ok" => false, "error" => "Not admin"]);
    exit;
}

$imageId = $_POST['image_id'] ?? null;
$ageDays = $_POST['age_days'] ?? null;

if (!$imageId || $ageDays === null) {
    echo json_encode(["ok" => false, "error" => "Missing fields"]);
    exit;
}

$ageDays = (int)$ageDays;
if ($ageDays < 0) $ageDays = 0;

$db = DB::get();

$db->prepare("UPDATE images SET age_days = ? WHERE id = ?")
   ->execute([$ageDays, $imageId]);

echo json_encode([
    "ok" => true,
    "image_id" => $imageId,
    "age_days" => $ageDays
]);