<?php
// document-delete.php - Eliminar un archivo de documento
include __DIR__ . '/core/autoload.php';

$con = Database::getCon();

$id = intval($_GET['id'] ?? 0);
if ($id <= 0) {
    header('Location: ./?view=documentation');
    exit;
}

$stmt = $con->prepare("SELECT file_path, document_type_id FROM document_file WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$file = $stmt->get_result()->fetch_assoc();

if ($file) {
    $fullPath = __DIR__ . '/' . $file['file_path'];
    if (file_exists($fullPath)) {
        unlink($fullPath);
    }
    $del = $con->prepare("DELETE FROM document_file WHERE id = ?");
    $del->bind_param('i', $id);
    $del->execute();
    header('Location: ./?view=documentation&type_id=' . $file['document_type_id']);
} else {
    header('Location: ./?view=documentation');
}
exit;
