<?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'])) {
    Response::error('image_id is required', 422);
}

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

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

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

/**
 * Soft delete
 */
$stmt = $db->prepare("
    UPDATE retail_product_images
    SET
        is_deleted = 1,
        status = 'InActive'
    WHERE id = :id
");

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

Response::success('Image deleted successfully');
