<?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);

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

$stmt = $db->prepare("SELECT image_path FROM retail_product_images WHERE id=:id");
$stmt->execute(['id'=>$imageId]);
$path = $stmt->fetchColumn();

if ($path && file_exists(__DIR__.'/../../../'.ltrim($path,'/'))) {
    unlink(__DIR__.'/../../../'.ltrim($path,'/'));
}

$db->prepare("DELETE FROM retail_product_images WHERE id=:id")->execute(['id'=>$imageId]);

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