<?php
require_once 'init.php';

// Suraksha jaanch: Sirf Admin hi is page ko access kar sakta hai.
if (!userHasRole(['Admin'])) {
    header("Location: dashboard.php");
    exit();
}

$message = '';
$message_type = '';
$final_script_path = '';

/** * SMART PATH DETECTION
 * Ye code automatic dhoondhega ki backup file kahan chhupi hai.
 */
$current_dir = __DIR__;
$parent_dir = dirname($current_dir); 

$possible_paths = [
    '/home/cybsoul/kids.eventsnews.in/auto_backup.php', // Aapki nayi location
    $parent_dir . '/backups/backup_ftp.php',           // Purani locations backup ke liye
    $current_dir . '/backups/backup_ftp.php',
    '/home6/cybsoul/backups/backup_ftp.php',
    '/home/cybsoul/backups/backup_ftp.php'
];

foreach ($possible_paths as $p) {
    if (file_exists($p)) {
        $final_script_path = $p;
        break;
    }
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['start_backup'])) {
    
    if (empty($final_script_path)) {
        $message = "❌ Error: Backup file nahi mili. Kripya check karein ki file sahi location par hai.";
        $message_type = 'danger';
    } else {
        // Log file ka path hamesha '/home/cybsoul/backups/' mein save karne ki koshish karein
        $log_path = '/home/cybsoul/backups/backup_debug_log.txt';
        if (!is_dir('/home/cybsoul/backups/')) {
             $log_path = dirname($final_script_path) . '/backup_debug_log.txt';
        }
        
        // Background Command
        $command = '/usr/local/bin/php ' . $final_script_path . ' > ' . $log_path . ' 2>&1 &';
        exec($command);
        
        $message = "✅ Success! Backup aur FTP transfer background mein start ho gaya hai (" . basename($final_script_path) . ").";
        $message_type = 'success';
    }
}

require_once 'header.php';
?>
<title>Manual Backup - Digify OS</title>

<div class="p-6 md:p-10 max-w-4xl mx-auto">
    <header class="mb-10">
        <h2 class="text-3xl font-bold text-slate-800 tracking-tight">System Backup & Sync</h2>
        <p class="text-slate-500 mt-1">Is tool se aap turant Database aur Files ka backup Server B (FTP) par bhej sakte hain.</p>
    </header>

    <?php if ($message): ?>
    <div class="mb-8 p-5 rounded-2xl border <?php echo ($message_type == 'success') ? 'bg-emerald-50 border-emerald-100 text-emerald-800' : 'bg-rose-50 border-rose-100 text-rose-800'; ?> flex items-start gap-4 shadow-sm">
        <div class="flex-shrink-0 w-6 h-6 rounded-full <?php echo ($message_type == 'success') ? 'bg-emerald-500' : 'bg-rose-500'; ?> text-white flex items-center justify-center mt-0.5 font-bold text-xs">
            <?php echo ($message_type == 'success') ? '✓' : '!'; ?>
        </div>
        <div class="text-sm font-medium">
            <?php echo htmlspecialchars($message); ?>
        </div>
    </div>
    <?php endif; ?>

    <div class="bg-white rounded-3xl shadow-xl border border-slate-100 overflow-hidden">
        <div class="p-8 md:p-12 text-center">
            <div class="inline-flex items-center justify-center w-20 h-20 bg-indigo-50 text-indigo-600 rounded-3xl mb-6 shadow-inner">
                <svg class="w-10 h-10" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
            </div>
            
            <h3 class="text-2xl font-bold text-slate-800 mb-4">Start Verified Backup</h3>
            
            <div class="mb-8 p-4 bg-slate-50 rounded-2xl inline-block text-left border border-slate-100">
                <p class="text-[10px] font-bold text-slate-400 uppercase tracking-widest mb-1">Detected Worker Path:</p>
                <code class="text-xs text-indigo-600 font-mono">
                    <?php echo $final_script_path ?: '<span class="text-rose-500">FILE NOT FOUND AT SPECIFIED LOCATION</span>'; ?>
                </code>
            </div>

            <form method="POST" action="manual_backup.php">
                <button type="submit" name="start_backup" <?php echo empty($final_script_path) ? 'disabled' : ''; ?> 
                        class="inline-flex items-center justify-center px-12 py-4 bg-indigo-600 text-white font-bold rounded-2xl shadow-lg hover:bg-indigo-700 transition-all active:scale-95 disabled:bg-slate-300 disabled:cursor-not-allowed">
                    Trigger Manual Backup & FTP
                </button>
            </form>
            
            <p class="mt-6 text-xs text-slate-400">
                Tip: Backup hone mein 2-5 minute lag sakte hain (File size par depend karta hai).
            </p>
        </div>
    </div>
</div>

<?php
require_once 'footer.php';
?>