<?php
/**
 * BACKUP JA TURVALISUSE SÜSTEEM
 * Kaitseb andmeid ja võimaldab taastamist
 */

require_once '../config/config.php';
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

// Nõua admin õigusi
requireAdmin();

$message = '';
$backup_files = [];

// Leia olemasolevad backup failid
$backup_dir = '../backups';
if (!is_dir($backup_dir)) {
    @mkdir($backup_dir, 0755, true);
}

if (is_dir($backup_dir)) {
    $files = glob($backup_dir . '/*.sql');
    foreach ($files as $file) {
        $backup_files[] = [
            'name' => basename($file),
            'path' => $file,
            'size' => filesize($file),
            'date' => filemtime($file)
        ];
    }
    // Sorteeri kuupäeva järgi, uusimad enne
    usort($backup_files, function($a, $b) {
        return $b['date'] - $a['date'];
    });
}

// BACKUP LOOMINE
if (isset($_POST['create_backup'])) {
    try {
        $backup_name = 'estreka_backup_' . date('Y-m-d_H-i-s') . '.sql';
        $backup_file = $backup_dir . '/' . $backup_name;
        
        // SQLite andmebaasi backup
        $db_path = BASE_PATH . '/data/estreka.db';
        
        if (file_exists($db_path)) {
            // Kopeeri andmebaasi fail
            $copy_result = copy($db_path, $backup_dir . '/estreka_db_' . date('Y-m-d_H-i-s') . '.db');
            
            // Genereeri ka SQL dump
            $sql_content = generateSQLDump($pdo);
            $sql_result = file_put_contents($backup_file, $sql_content);
            
            if ($copy_result && $sql_result) {
                $message = "✅ Backup edukalt loodud: $backup_name (" . formatBytes($sql_result) . ")";
                logActivity($_SESSION['user_id'], 'backup_created', "Created backup: $backup_name");
                
                // Uuenda backup failide nimekirja
                header('Location: ' . $_SERVER['REQUEST_URI']);
                exit;
            } else {
                $message = "❌ Backup loomine ebaõnnestus";
            }
        } else {
            $message = "❌ Andmebaasi faili ei leitud: $db_path";
        }
        
    } catch (Exception $e) {
        $message = "❌ Backup viga: " . $e->getMessage();
        error_log('Backup error: ' . $e->getMessage());
    }
}

// BACKUP TAASTAMINE
if (isset($_POST['restore_backup']) && !empty($_POST['backup_file'])) {
    $backup_file = $_POST['backup_file'];
    $backup_path = $backup_dir . '/' . $backup_file;
    
    if (file_exists($backup_path) && pathinfo($backup_file, PATHINFO_EXTENSION) === 'sql') {
        try {
            // Loo backup enne taastamist
            $safety_backup = $backup_dir . '/before_restore_' . date('Y-m-d_H-i-s') . '.sql';
            $safety_content = generateSQLDump($pdo);
            file_put_contents($safety_backup, $safety_content);
            
            // Taasta andmebaas
            $sql_content = file_get_contents($backup_path);
            
            // Käivita SQL käsud
            $pdo->exec('PRAGMA foreign_keys = OFF');
            $pdo->exec($sql_content);
            $pdo->exec('PRAGMA foreign_keys = ON');
            
            $message = "✅ Andmebaas taastatud failist: $backup_file (Loodud ka safety backup)";
            logActivity($_SESSION['user_id'], 'backup_restored', "Restored from backup: $backup_file");
            
        } catch (Exception $e) {
            $message = "❌ Taastamise viga: " . $e->getMessage();
            error_log('Restore error: ' . $e->getMessage());
        }
    } else {
        $message = "❌ Backup faili ei leitud või vale formaat";
    }
}

// BACKUP KUSTUTAMINE
if (isset($_POST['delete_backup']) && !empty($_POST['backup_file'])) {
    $backup_file = $_POST['backup_file'];
    $backup_path = $backup_dir . '/' . $backup_file;
    
    if (file_exists($backup_path)) {
        if (unlink($backup_path)) {
            $message = "✅ Backup fail kustutatud: $backup_file";
            logActivity($_SESSION['user_id'], 'backup_deleted', "Deleted backup: $backup_file");
            
            // Uuenda nimekirja
            header('Location: ' . $_SERVER['REQUEST_URI']);
            exit;
        } else {
            $message = "❌ Backup faili kustutamine ebaõnnestus";
        }
    } else {
        $message = "❌ Backup faili ei leitud";
    }
}

// SQL DUMP GENEREERIMINE - TÄIUSTATUD DÜNAAMILINE VERSIOON
function generateSQLDump($pdo) {
    $sql = "-- Estreka Ratsaklubi Database Backup\n";
    $sql .= "-- Created: " . date('Y-m-d H:i:s') . "\n";
    $sql .= "-- Database: SQLite\n";
    $sql .= "-- Version: " . $pdo->query('SELECT sqlite_version()')->fetchColumn() . "\n\n";
    
    $sql .= "-- Disable foreign key checks during restore\n";
    $sql .= "PRAGMA foreign_keys = OFF;\n\n";
    
    // 1. LEIA KÕIK TABELID (dünaamiliselt)
    $tables = $pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name")->fetchAll(PDO::FETCH_COLUMN);
    
    $sql .= "-- Found " . count($tables) . " tables: " . implode(', ', $tables) . "\n\n";
    
    foreach ($tables as $table) {
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        $sql .= "-- Table: $table\n";
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        
        // 2. TABELI STRUKTUUR (dünaamiliselt)
        $create_sql = $pdo->query("SELECT sql FROM sqlite_master WHERE name='$table' AND type='table'")->fetchColumn();
        $sql .= "DROP TABLE IF EXISTS `$table`;\n";
        $sql .= $create_sql . ";\n\n";
        
        // 3. TABELI ANDMED (dünaamiliselt)
        try {
            $stmt = $pdo->query("SELECT COUNT(*) FROM `$table`");
            $row_count = $stmt->fetchColumn();
            
            $sql .= "-- Table `$table` contains $row_count rows\n";
            
            if ($row_count > 0) {
                $rows = $pdo->query("SELECT * FROM `$table`")->fetchAll(PDO::FETCH_ASSOC);
                
                $sql .= "-- Data for table: $table\n";
                
                foreach ($rows as $row) {
                    $columns = array_keys($row);
                    $values = array_map(function($value) use ($pdo) {
                        return $value === null ? 'NULL' : $pdo->quote($value);
                    }, array_values($row));
                    
                    $sql .= "INSERT INTO `$table` (`" . implode('`, `', $columns) . "`) VALUES (" . implode(', ', $values) . ");\n";
                }
                $sql .= "\n";
            } else {
                $sql .= "-- No data in table `$table`\n\n";
            }
            
        } catch (Exception $e) {
            $sql .= "-- ERROR reading table `$table`: " . $e->getMessage() . "\n\n";
        }
    }
    
    // 4. INDEKSID (dünaamiliselt)
    $indexes = $pdo->query("SELECT name, sql FROM sqlite_master WHERE type='index' AND name NOT LIKE 'sqlite_%' AND sql IS NOT NULL ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
    
    if (!empty($indexes)) {
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        $sql .= "-- INDEXES (" . count($indexes) . " found)\n";
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        
        foreach ($indexes as $index) {
            $sql .= "-- Index: " . $index['name'] . "\n";
            $sql .= $index['sql'] . ";\n\n";
        }
    }
    
    // 5. TRIGGERID (dünaamiliselt) 
    $triggers = $pdo->query("SELECT name, sql FROM sqlite_master WHERE type='trigger' ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
    
    if (!empty($triggers)) {
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        $sql .= "-- TRIGGERS (" . count($triggers) . " found)\n";
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        
        foreach ($triggers as $trigger) {
            $sql .= "-- Trigger: " . $trigger['name'] . "\n";
            $sql .= $trigger['sql'] . ";\n\n";
        }
    }
    
    // 6. VIEWS (dünaamiliselt)
    $views = $pdo->query("SELECT name, sql FROM sqlite_master WHERE type='view' ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
    
    if (!empty($views)) {
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        $sql .= "-- VIEWS (" . count($views) . " found)\n";
        $sql .= "-- " . str_repeat("=", 50) . "\n";
        
        foreach ($views as $view) {
            $sql .= "-- View: " . $view['name'] . "\n";
            $sql .= "DROP VIEW IF EXISTS `" . $view['name'] . "`;\n";
            $sql .= $view['sql'] . ";\n\n";
        }
    }
    
    // 7. LÕPETA
    $sql .= "-- Re-enable foreign key checks\n";
    $sql .= "PRAGMA foreign_keys = ON;\n\n";
    
    $sql .= "-- Backup completed successfully\n";
    $sql .= "-- Total tables: " . count($tables) . "\n";
    $sql .= "-- Total indexes: " . count($indexes) . "\n";
    $sql .= "-- Total triggers: " . count($triggers) . "\n";
    $sql .= "-- Total views: " . count($views) . "\n";
    
    return $sql;
}

// FAILI SUURUSE FORMAATIMINE
function formatBytes($size, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB');
    
    for ($i = 0; $size > 1024 && $i < count($units) - 1; $i++) {
        $size /= 1024;
    }
    
    return round($size, $precision) . ' ' . $units[$i];
}

// Lehe andmed
$page_title = 'Backup ja turvalisus';
$page_description = 'Andmete varundamine ja taastamine turvalisuse tagamiseks.';
$body_class = 'backup-page';

// Kaasaegne CSS ainult backup lehele
$extra_head_content = '
<style>
/* BACKUP PAGE SCOPED CSS */
.backup-main {
    padding: var(--spacing-6) 0;
    background: var(--gray-50);
    min-height: calc(100vh - 140px);
}

.backup-main .breadcrumb {
    display: flex;
    align-items: center;
    gap: var(--spacing-2);
    margin-bottom: var(--spacing-6);
    font-size: var(--font-size-sm);
}

.backup-main .breadcrumb a {
    color: var(--primary-color);
    text-decoration: none;
}

.backup-main .breadcrumb-separator {
    color: var(--gray-400);
}

.backup-main .breadcrumb-current {
    color: var(--gray-600);
}

.backup-main .page-header {
    margin-bottom: var(--spacing-8);
}

.backup-main .page-title {
    display: flex;
    align-items: center;
    gap: var(--spacing-3);
    margin-bottom: var(--spacing-2);
    font-size: 2rem;
    font-weight: 600;
    color: var(--gray-900);
}

.backup-main .page-subtitle {
    color: var(--gray-600);
    font-size: var(--font-size-lg);
    margin: 0;
}

.backup-main .backup-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: var(--spacing-6);
    margin-bottom: var(--spacing-8);
}

.backup-main .backup-card {
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
    border: 1px solid var(--gray-200);
    transition: all var(--transition-fast);
}

.backup-main .backup-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.backup-main .card-header {
    padding: var(--spacing-5) var(--spacing-6);
    border-bottom: 1px solid var(--gray-200);
    background: var(--gray-50);
}

.backup-main .card-title {
    display: flex;
    align-items: center;
    gap: var(--spacing-3);
    margin: 0 0 var(--spacing-2);
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--gray-900);
}

.backup-main .card-description {
    color: var(--gray-600);
    font-size: var(--font-size-sm);
    margin: 0;
    line-height: 1.5;
}

.backup-main .card-content {
    padding: var(--spacing-6);
}

.backup-main .backup-form {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-4);
}

.backup-main .info-box {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: var(--spacing-4);
    margin: var(--spacing-4) 0;
}

.backup-main .info-box strong {
    color: var(--gray-900);
    display: block;
    margin-bottom: var(--spacing-2);
}

.backup-main .info-box ul {
    margin: var(--spacing-2) 0 0;
    padding-left: var(--spacing-5);
}

.backup-main .info-box li {
    margin-bottom: var(--spacing-1);
    color: var(--gray-700);
    font-size: var(--font-size-sm);
}

.backup-main .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-2);
    padding: var(--spacing-3) var(--spacing-4);
    border-radius: var(--radius);
    font-size: var(--font-size-sm);
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all var(--transition-fast);
    border: 1px solid transparent;
    width: 100%;
}

.backup-main .btn-primary {
    background: var(--primary-color);
    color: white;
}

.backup-main .btn-primary:hover {
    background: var(--primary-dark);
}

.backup-main .btn-success {
    background: var(--success-color);
    color: white;
}

.backup-main .btn-success:hover {
    background: #059669;
}

.backup-main .btn-danger {
    background: var(--danger-color);
    color: white;
}

.backup-main .btn-danger:hover {
    background: #dc2626;
}

.backup-main .btn-secondary {
    background: var(--gray-600);
    color: white;
}

.backup-main .btn-secondary:hover {
    background: var(--gray-700);
}

.backup-main .btn-sm {
    padding: var(--spacing-2) var(--spacing-3);
    font-size: var(--font-size-xs);
    width: auto;
}

.backup-main .backup-files {
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    border: 1px solid var(--gray-200);
}

.backup-main .backup-files-header {
    padding: var(--spacing-5) var(--spacing-6);
    border-bottom: 1px solid var(--gray-200);
    background: var(--gray-50);
}

.backup-main .backup-files-title {
    margin: 0;
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--gray-900);
}

.backup-main .backup-files-content {
    padding: 0;
}

.backup-main .backup-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--spacing-4) var(--spacing-6);
    border-bottom: 1px solid var(--gray-100);
    transition: background-color var(--transition-fast);
}

.backup-main .backup-item:last-child {
    border-bottom: none;
}

.backup-main .backup-item:hover {
    background: var(--gray-50);
}

.backup-main .backup-info {
    flex: 1;
}

.backup-main .backup-name {
    font-weight: 500;
    color: var(--gray-900);
    margin-bottom: var(--spacing-1);
    font-size: var(--font-size-sm);
}

.backup-main .backup-meta {
    display: flex;
    gap: var(--spacing-4);
    font-size: var(--font-size-xs);
    color: var(--gray-500);
}

.backup-main .backup-actions {
    display: flex;
    gap: var(--spacing-2);
    flex-shrink: 0;
    margin-left: var(--spacing-4);
}

.backup-main .empty-state {
    text-align: center;
    padding: var(--spacing-12) var(--spacing-6);
    color: var(--gray-500);
}

.backup-main .empty-state .empty-icon {
    font-size: 3rem;
    margin-bottom: var(--spacing-4);
}

.backup-main .alert {
    padding: var(--spacing-4);
    border-radius: var(--radius);
    margin-bottom: var(--spacing-6);
    display: flex;
    align-items: center;
    gap: var(--spacing-2);
    border: 1px solid;
}

.backup-main .alert-success {
    background: color-mix(in srgb, var(--success-color) 10%, transparent);
    border-color: color-mix(in srgb, var(--success-color) 30%, transparent);
    color: #065f46;
}

.backup-main .alert-error {
    background: color-mix(in srgb, var(--danger-color) 10%, transparent);
    border-color: color-mix(in srgb, var(--danger-color) 30%, transparent);
    color: #dc2626;
}

.backup-main .alert-warning {
    background: color-mix(in srgb, var(--warning-color) 10%, transparent);
    border-color: color-mix(in srgb, var(--warning-color) 30%, transparent);
    color: #92400e;
}

.backup-main .info-section {
    background: white;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    border: 1px solid var(--gray-200);
    margin-top: var(--spacing-8);
}

.backup-main .info-section-header {
    padding: var(--spacing-5) var(--spacing-6);
    border-bottom: 1px solid var(--gray-200);
    background: var(--gray-50);
}

.backup-main .info-section-title {
    margin: 0;
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--gray-900);
}

.backup-main .info-section-content {
    padding: var(--spacing-6);
}

.backup-main .warning-box {
    background: color-mix(in srgb, var(--warning-color) 5%, transparent);
    border: 1px solid color-mix(in srgb, var(--warning-color) 20%, transparent);
    border-radius: var(--radius);
    padding: var(--spacing-5);
    margin: var(--spacing-6) 0;
}

.backup-main .warning-box h4 {
    margin: 0 0 var(--spacing-3);
    color: #92400e;
    font-size: var(--font-size-base);
}

.backup-main .warning-box ul {
    margin: 0;
    padding-left: var(--spacing-5);
}

.backup-main .warning-box li {
    margin-bottom: var(--spacing-2);
    color: #92400e;
    line-height: 1.5;
}

/* Icons */
.backup-main .icon-shield:before { content: "🛡️"; }
.backup-main .icon-save:before { content: "💾"; }
.backup-main .icon-clock:before { content: "⏰"; }
.backup-main .icon-folder:before { content: "📂"; }
.backup-main .icon-file:before { content: "📄"; }
.backup-main .icon-calendar:before { content: "📅"; }
.backup-main .icon-chart:before { content: "📊"; }
.backup-main .icon-time:before { content: "⏰"; }
.backup-main .icon-refresh:before { content: "🔄"; }
.backup-main .icon-download:before { content: "📥"; }
.backup-main .icon-trash:before { content: "🗑️"; }
.backup-main .icon-cog:before { content: "⚙️"; }

/* Responsive */
@media (max-width: 768px) {
    .backup-main .backup-grid {
        grid-template-columns: 1fr;
    }
    
    .backup-main .backup-item {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--spacing-3);
    }
    
    .backup-main .backup-actions {
        margin-left: 0;
        width: 100%;
    }
    
    .backup-main .backup-meta {
        flex-direction: column;
        gap: var(--spacing-1);
    }
    
    .backup-main .btn {
        flex: 1;
    }
}
</style>
';

include '../includes/header.php';
?>

<main class="backup-main">
    <div class="container">
        <!-- Breadcrumb -->
        <nav class="breadcrumb">
            <a href="<?php echo url('admin/dashboard.php'); ?>">Admin paneel</a>
            <span class="breadcrumb-separator">→</span>
            <span class="breadcrumb-current">Backup ja turvalisus</span>
        </nav>

        <!-- Header -->
        <div class="page-header">
            <h1 class="page-title">
                <i class="icon-shield"></i>
                Backup ja turvalisus
            </h1>
            <p class="page-subtitle">
                Andmete varundamine ja taastamine. Hoia oma andmed kaitstud!
            </p>
        </div>

        <!-- Alerts -->
        <?php if ($message): ?>
            <div class="alert <?php echo strpos($message, '✅') !== false ? 'alert-success' : 'alert-error'; ?>">
                <span><?php echo htmlspecialchars($message); ?></span>
            </div>
        <?php endif; ?>

        <!-- Backup toimingud -->
        <div class="backup-grid">
            <!-- Backup loomine -->
            <div class="backup-card">
                <div class="card-header">
                    <h2 class="card-title">
                        <i class="icon-save"></i>
                        Loo uus backup
                    </h2>
                    <p class="card-description">
                        Salvestab kogu andmebaasi turvalisesse backup faili.
                    </p>
                </div>
                <div class="card-content">
                    <form method="POST" class="backup-form">
                        <button type="submit" name="create_backup" class="btn btn-success" 
                                onclick="return confirm('Kas luua uus backup?')">
                            <i class="icon-save"></i>
                            <span>Loo backup kohe</span>
                        </button>
                    </form>
                    
                    <div class="info-box">
                        <strong>Mis salvestatakse:</strong>
                        <ul>
                            <li>Kõik kasutajad ja nende andmed</li>
                            <li>Kõik võistlustulemused</li>
                            <li>Treeningu päevikud</li>
                            <li>Süsteemi seaded</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Automaatne backup -->
            <div class="backup-card">
                <div class="card-header">
                    <h2 class="card-title">
                        <i class="icon-clock"></i>
                        Automaatne backup
                    </h2>
                    <p class="card-description">
                        Soovitame seadistada iga-päevane automaatne backup.
                    </p>
                </div>
                <div class="card-content">
                    <div class="info-box">
                        <strong>Hosting cPanel:</strong><br>
                        1. Cron Jobs → Add New<br>
                        2. Kord päevas: <code>0 2 * * *</code><br>
                        3. Käsk: <code>php /path/to/backup.php</code>
                    </div>
                    <button class="btn btn-primary" onclick="alert('Võta ühendust hostingu pakkujaga automaatse backup seadistamiseks!')">
                        <i class="icon-cog"></i>
                        <span>Seadista automaatne backup</span>
                    </button>
                </div>
            </div>
        </div>

        <!-- Backup failide nimekiri -->
        <div class="backup-files">
            <div class="backup-files-header">
                <h3 class="backup-files-title">
                    <i class="icon-folder"></i>
                    Olemasolevad backup failid (<?php echo count($backup_files); ?>)
                </h3>
            </div>
            <div class="backup-files-content">
                <?php if (empty($backup_files)): ?>
                    <div class="empty-state">
                        <div class="empty-icon">📁</div>
                        <p>Backup faile ei leitud!</p>
                        <p><small>Loo esimene backup üleval olevast nupust.</small></p>
                    </div>
                <?php else: ?>
                    <?php foreach ($backup_files as $backup): ?>
                        <div class="backup-item">
                            <div class="backup-info">
                                <div class="backup-name">
                                    <i class="icon-file"></i>
                                    <?php echo htmlspecialchars($backup['name']); ?>
                                </div>
                                <div class="backup-meta">
                                    <span><i class="icon-calendar"></i> <?php echo date('d.m.Y H:i:s', $backup['date']); ?></span>
                                    <span><i class="icon-chart"></i> <?php echo formatBytes($backup['size']); ?></span>
                                    <span><i class="icon-time"></i> <?php echo date('H:i:s \t\e\m\m\u \t\a\g\a\s\i', time() - $backup['date']); ?></span>
                                </div>
                            </div>
                            <div class="backup-actions">
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="backup_file" value="<?php echo htmlspecialchars($backup['name']); ?>">
                                    <button type="submit" name="restore_backup" class="btn btn-primary btn-sm"
                                            onclick="return confirm('HOIATUS: See kustutab kõik praegused andmed ja taastab backup! Kas oled kindel?')">
                                        <i class="icon-refresh"></i>
                                        <span>Taasta</span>
                                    </button>
                                </form>
                                <a href="<?php echo url('backups/' . $backup['name']); ?>" class="btn btn-secondary btn-sm" download>
                                    <i class="icon-download"></i>
                                    <span>Laadi alla</span>
                                </a>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="backup_file" value="<?php echo htmlspecialchars($backup['name']); ?>">
                                    <button type="submit" name="delete_backup" class="btn btn-danger btn-sm"
                                            onclick="return confirm('Kas kustutada backup fail?')">
                                        <i class="icon-trash"></i>
                                        <span>Kustuta</span>
                                    </button>
                                </form>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- Hoiatused ja juhised -->
        <div class="info-section">
            <div class="info-section-header">
                <h3 class="info-section-title">⚠️ Olulised hoiatused</h3>
            </div>
            <div class="info-section-content">
                <div class="warning-box">
                    <h4>Enne backup kasutamist loe hoolikalt:</h4>
                    <ul>
                        <li><strong>Backup taastamine kustutab kõik praegused andmed!</strong></li>
                        <li>Enne taastamist luuakse automaatselt safety backup</li>
                        <li>Backup failid sisaldavad tundlikku infot - hoia turvaliselt</li>
                        <li>Testi backup taastamist enne kui seda päriselt vajad</li>
                        <li>Laadi backup failid regulaarselt alla eraldi kohta</li>
                    </ul>
                </div>
                
                <h4>📋 Kuidas kasutada backup süsteemi</h4>
                <ol>
                    <li><strong>Loo backup enne muudatusi:</strong> Alati enne suuremaid muudatusi või uuendusi</li>
                    <li><strong>Regulaarne backup:</strong> Vähemalt kord nädalas või pärast iga võistlust</li>
                    <li><strong>Laadi failid alla:</strong> Hoia backup faile ka väljaspool serverit</li>
                    <li><strong>Testi taastamist:</strong> Proovi aeg-ajalt backup taastamist test keskkonnas</li>
                    <li><strong>Nummerdatud backupid:</strong> Säilita mitu vanemat backup versiooni</li>
                </ol>
                
                <h4>🚨 Millal kasutada backup taastamist</h4>
                <ul>
                    <li>Andmed on kogemata kustutatud</li>
                    <li>Andmebaas on rikutud või korrumpeerunud</li>
                    <li>Vigane uuendus või import on andmed ära rikkunud</li>
                    <li>Serveri või riistvara viga</li>
                    <li>Vaja tagasi minna varasemasse süsteemi versiooni</li>
                </ul>
            </div>
        </div>

        <!-- Järgmised sammud -->
        <div class="info-section">
            <div class="info-section-header">
                <h3 class="info-section-title">💡 Järgmised sammud kadunud andmete taastamiseks</h3>
            </div>
            <div class="info-section-content">
                <ol style="color: var(--success-color); margin: 0;">
                    <li><strong>Kontrolli hostingu backup:</strong> Võib-olla hosting teeb automaatset backup</li>
                    <li><strong>Otsi e-mail logidest:</strong> Activity_log tabelis võib olla kasutaja infot</li>
                    <li><strong>Paberkandjad:</strong> Kas võistluste tulemused on kusagil kirjas?</li>
                    <li><strong>Loo uus kasutaja:</strong> Sama andmetega ja sisesta tulemused uuesti</li>
                    <li><strong>Sea üles automaatne backup:</strong> Et see enam ei juhtuks</li>
                </ol>
            </div>
        </div>
    </div>
</main>

<?php include '../includes/footer.php'; ?>