<?php
include 'config.php';

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

// Folder Choose
$type = $_POST['type']; 
if ($type === 'shram') {
    $folder = APP_DIR;
} elseif ($type === 'admin') {
    $folder = APP_DIR_ADMIN;
} else {
    die(json_encode(["error" => "Invalid type parameter"]));
}

if (isset($_FILES['file'])) {
    $filename = basename($_FILES["file"]["name"]);
    $target_file = $folder . $filename;
     // ✅ फाइल पहिले नै छ कि भनेर जाँच
    if (file_exists($target_file)) {
        echo json_encode(["error" => "फाइल पहिल्यै Server मा छ!"]);
        exit;
    }


    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo json_encode(["success" => "File uploaded"]);
    } else {
        echo json_encode(["error" => "Upload failed"]);
    }
} else {
    echo json_encode(["error" => "No file received"]);
}
?>
