<?php
declare(strict_types=1);

require_once __DIR__.'/../../security/auth_guard.php';
require_once __DIR__.'/../../security/csrf_guard.php';
require_once __DIR__.'/../../config/database.php';
require_once __DIR__.'/../../core/Response.php';

$data = json_decode(file_get_contents('php://input'), true);

if (!is_array($data)) {
    Response::error('Invalid JSON', 400);
}

$imageId = $data['image_id'] ?? null;
$status  = $data['status'] ?? null;

if (!is_numeric($imageId)) {
    Response::error('image_id is required', 422);
}

if (!in_array($status, ['Active','Inactive'], true)) {
    Response::error('Invalid status', 422);
}

$db = Database::getInstance();

$stmt = $db->prepare("
    UPDATE dealer_images
    SET status = :status
    WHERE id = :id AND is_deleted = 0
    LIMIT 1
");

$stmt->execute([
    'status' => $status,
    'id'     => (int)$imageId
]);

if ($stmt->rowCount() === 0) {
    Response::error('Image not found or already deleted', 404);
}

Response::success('Image status updated');
