<?php
require_once 'auth.php';
include 'db.php';
requireLogin($conn);

header('Content-Type: application/json; charset=utf-8');

$folder_id = intval($_GET['folder_id'] ?? ($_GET['id'] ?? 0));

if ($folder_id <= 0) {
    http_response_code(400);
    echo json_encode([
        'exists' => false,
        'message' => 'ID fail tidak sah.'
    ]);
    exit();
}

$stmt = mysqli_prepare($conn, "SELECT id, folder_name, folder_code, closed_at FROM folders WHERE id = ? AND deleted_at IS NULL");
mysqli_stmt_bind_param($stmt, "i", $folder_id);
mysqli_stmt_execute($stmt);
$folder = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));

if (!$folder) {
    http_response_code(404);
    echo json_encode([
        'exists' => false,
        'message' => 'Fail tidak lagi wujud atau telah dipadam.'
    ]);
    exit();
}

echo json_encode([
    'exists' => true,
    'closed' => !empty($folder['closed_at']),
    'folder_id' => (int) $folder['id'],
    'folder_name' => $folder['folder_name'],
    'folder_code' => $folder['folder_code'],
    'message' => !empty($folder['closed_at']) ? 'Fail ini telah ditutup.' : 'Fail aktif.'
]);

