<?php
include 'config.php';

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

// Folder Choose based on 'type'
$type = $_POST['type']; // Expected: 'anugaman', 'mudda', 'db', 'app'
$folder = ($type === 'anugaman') ? ANUGAMAN_DIR :
          (($type === 'mudda') ? MUDDA_DIR :
          (($type === 'db') ? DB_DIR : APP_DIR));

// Create folder if not exists
if (!is_dir($folder)) {
    mkdir($folder, 0777, true);
}

// For response
$response = [];

// Handle DB file
if (isset($_FILES['file'])) {
    $filename = basename($_FILES["file"]["name"]);
    $target_file = $folder . $filename;

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        $response['db'] = "✅ डाटाबेस Upload सफल";
    } else {
        $response['db'] = "❌ डाटाबेस Upload असफल";
    }
} else {
    $response['db'] = "❌ डाटाबेस फाइल प्राप्त भएन";
}

// Handle Labour file (optional)
if (isset($_FILES['labour'])) {
    // Ensure labourdetails folder exists
    if (!is_dir(LABOURDETAILS)) {
        mkdir(LABOURDETAILS, 0777, true);
    }

    $labourName = basename($_FILES["labour"]["name"]);
    $labourTarget = LABOURDETAILS . $labourName;

    if (move_uploaded_file($_FILES["labour"]["tmp_name"], $labourTarget)) {
        $response['labour'] = "✅ श्रमिक फाइल Upload सफल";
    } else {
        $response['labour'] = "❌ श्रमिक फाइल Upload असफल";
    }
} else {
    $response['labour'] = "ℹ️ श्रमिक फाइल प्राप्त भएन (optional)";
}

echo json_encode($response);



// include 'config.php';

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

// // Folder Choose
// $type = $_POST['type']; // 'anugaman', 'mudda', 'db', 'app', 'labour'
// $folder = ($type === 'anugaman') ? ANUGAMAN_DIR :
//           (($type === 'mudda') ? MUDDA_DIR :
//           (($type === 'db') ? DB_DIR :
//           (($type === 'labour') ? LABOURDETAILS : APP_DIR)));

// if (isset($_FILES['file'])) {
//     $filename = basename($_FILES["file"]["name"]);
//     $target_file = $folder . $filename;

//     // Create folder if it doesn't exist
//     if (!is_dir($folder)) {
//         mkdir($folder, 0777, true);
//     }

//     if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
//         echo json_encode(["success" => "✅ Upload सफल"]);
//     } else {
//         echo json_encode(["error" => "❌ Upload असफल"]);
//     }
// } else {
//     echo json_encode(["error" => "❌ फाइल प्राप्त भएन"]);
// }
?>