<?php
require_once 'db/config.php';

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('Location: info_producto.php');
    exit;
}

$info_id = $_GET['id'];
$pdo = db();

// First, get the image URL to delete the file
$stmt_select = $pdo->prepare('SELECT imagen_url FROM info_productos WHERE id = :id');
$stmt_select->execute([':id' => $info_id]);
$info = $stmt_select->fetch(PDO::FETCH_ASSOC);

if ($info) {
    // Delete the database record
    $stmt_delete = $pdo->prepare('DELETE FROM info_productos WHERE id = :id');
    if ($stmt_delete->execute([':id' => $info_id])) {
        // If DB deletion is successful, delete the image file
        if (file_exists($info['imagen_url'])) {
            unlink($info['imagen_url']);
        }
        $_SESSION['success_message'] = 'La tarjeta de información ha sido eliminada.';
    } else {
        $_SESSION['error_message'] = 'No se pudo eliminar la tarjeta de la base de datos.';
    }
} else {
    $_SESSION['error_message'] = 'No se encontró la tarjeta especificada.';
}

header('Location: info_producto.php');
exit;