<?php
include 'config.php';

// Token Verify
if ($_GET['auth_token'] !== AUTH_TOKEN) {
    die(json_encode(["error" => "Unauthorized Access"]));
}

// Get Filename & Type
$filename = $_GET['filename'] ?? '';
$type = $_GET['type'] ?? '';

// Determine folder based on type
switch ($type) {
    case 'anugaman':
        $folder = ANUGAMAN_DIR;
        break;
    case 'mudda':
        $folder = MUDDA_DIR;
        break;
    case 'db':
        $folder = DB_DIR;
        break;
    case 'labour':
        $folder = LABOURDETAILS;
        break;
    default:
        $folder = APP_DIR; // fallback
}

// Full path
$file_path = $folder . $filename;

// Serve file
if (file_exists($file_path)) {
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    readfile($file_path);
} else {
    echo json_encode(["error" => "File not found"]);
}
?>


// include 'config.php';

// // Token Verify
// if ($_GET['auth_token'] !== AUTH_TOKEN) {
//     die(json_encode(["error" => "Unauthorized Access"]));
// }

// // Get Filename & Type
// $filename = $_GET['filename'];
// $type = $_GET['type'];

// $folder = ($type === 'anugaman') ? ANUGAMAN_DIR : (($type === 'mudda') ? MUDDA_DIR : (($type === 'db') ? DB_DIR : APP_DIR));
// $file_path = $folder . $filename;

// if (file_exists($file_path)) {
//     header("Content-Type: application/octet-stream");
//     header("Content-Disposition: attachment; filename=\"$filename\"");
//     readfile($file_path);
// } else {
//     echo json_encode(["error" => "File not found"]);
// }
?>
