<?php
// ============================================================
// backend/debug-upload.php
// TEMPORARY DEBUG FILE — DELETE AFTER FIXING THE ISSUE
// Visit: http://localhost/healthagency/backend/debug-upload.php
// ============================================================

// Disable auth for this debug file
header('Content-Type: application/json');

$uploadDir = dirname(__FILE__) . '/uploads/';
$scriptDir = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
$scheme    = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$uploadUrl = $scheme . '://' . $_SERVER['HTTP_HOST'] . $scriptDir . '/uploads/';

$info = [
    '__FILE__'              => __FILE__,
    '__DIR__'               => __DIR__,
    'SCRIPT_NAME'           => $_SERVER['SCRIPT_NAME'],
    'scriptDir (dirname)'   => $scriptDir,
    'uploadDir (computed)'  => $uploadDir,
    'uploadUrl (computed)'  => $uploadUrl,
    'uploads_dir_exists'    => is_dir($uploadDir),
    'uploads_dir_writable'  => is_writable($uploadDir),
    'uploads_dir_contents'  => is_dir($uploadDir) ? scandir($uploadDir) : 'N/A',

    // From UploadController path logic
    'controller_path'       => dirname(__FILE__) . '/api/uploads/UploadController.php',
    'controller___DIR__'    => dirname(__FILE__) . '/api/uploads',
    'dirname___DIR__,2'     => dirname(dirname(__FILE__) . '/api/uploads', 2),
    'controller_uploadDir'  => dirname(dirname(__FILE__) . '/api/uploads', 2) . '/uploads/',
    'controller_dir_exists' => is_dir(dirname(dirname(__FILE__) . '/api/uploads', 2) . '/uploads/'),
    'controller_writable'   => is_writable(dirname(dirname(__FILE__) . '/api/uploads', 2) . '/uploads/'),

    'php_upload_max_filesize' => ini_get('upload_max_filesize'),
    'php_post_max_size'       => ini_get('post_max_size'),
    'php_file_uploads'        => ini_get('file_uploads'),
    'php_tmp_dir'             => sys_get_temp_dir(),
    'php_tmp_writable'        => is_writable(sys_get_temp_dir()),
];

echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
