<?php
declare(strict_types=1);

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

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

if (empty($data['image_id']) || empty($data['status'])) {
    Response::error('image_id and status are required', 422);
}

$imageId = (int)$data['image_id'];
$status  = $data['status'];

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

/**
 * Validate image exists
 */
$stmt = $db->prepare("
    SELECT id FROM retail_product_images
    WHERE id = :id
");
$stmt->execute(['id' => $imageId]);

if (!$stmt->fetch()) {
    Response::error('Image not found', 404);
}

/**
 * Update status
 */
$stmt = $db->prepare("
    UPDATE retail_product_images
    SET
        status = :status
    WHERE id = :id
");

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

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