<?php
require_once 'includes/auth.php';
require_once 'config/database.php';
require_once 'includes/functions.php';

header('Content-Type: application/json');

$image_id = filter_input(INPUT_POST, 'image_id', FILTER_VALIDATE_INT);
if (!$image_id) {
    echo json_encode(['success' => false, 'error' => 'ID image manquant.']);
    exit;
}

$pdo = getPDO();
$stmt = $pdo->prepare("SELECT image_path FROM fiches_images WHERE id = ?");
$stmt->execute([$image_id]);
$path = $stmt->fetchColumn();

if ($path && file_exists($path)) {
    unlink($path);
}

$stmt = $pdo->prepare("DELETE FROM fiches_images WHERE id = ?");
if ($stmt->execute([$image_id])) {
    logAction($pdo, $_SESSION['user_id'], 'delete_image_fiche', "Image ID $image_id");
    echo json_encode(['success' => true]);
} else {
    echo json_encode(['success' => false, 'error' => 'Erreur lors de la suppression.']);
}