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

$error = '';
$success = '';
$step = 'verifying'; // verifying, success, error

// Kontrolli tokenit
$token = getGet('token');

if (empty($token)) {
    $error = 'Kinnituslink on vigane või puudub.';
    $step = 'error';
} else {
    try {
        // Otsi kasutajat tokeni järgi
        $stmt = $pdo->prepare("
            SELECT id, firstName, lastName, email, verification_expires 
            FROM users 
            WHERE verification_token = ? AND email_verified = 0
        ");
        $stmt->execute([$token]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$user) {
            $error = 'Kinnituslink on vigane või konto on juba kinnitatud.';
            $step = 'error';
        } elseif (strtotime($user['verification_expires']) < time()) {
            $error = 'Kinnituslink on aegunud. Palun registreeru uuesti.';
            $step = 'error';
        } else {
            // Kinnita e-mail
            $stmt = $pdo->prepare("
                UPDATE users 
                SET email_verified = 1, 
                    isActive = 1, 
                    verification_token = NULL, 
                    verification_expires = NULL,
                    updatedAt = CURRENT_TIMESTAMP
                WHERE id = ?
            ");
            $stmt->execute([$user['id']]);
            
            $success = 'E-mail edukalt kinnitatud! Nüüd saad sisse logida.';
            $step = 'success';
            
            // Logi tegevus
            logActivity($user['id'], 'email_verified', 'Email address verified');
        }
        
    } catch(PDOException $e) {
        $error = 'E-maili kinnitamine ebaõnnestus. Palun proovi hiljem uuesti.';
        $step = 'error';
        error_log('Email verification error: ' . $e->getMessage());
    }
}

$page_title = 'E-maili kinnitamine';
$page_description = 'Kinnita oma e-maili aadress Estreka Ratsaklubi süsteemis.';
$body_class = 'verify-email-page';

include 'includes/header.php';
?>

<main class="auth-main">
    <div class="auth-container">
        <div class="auth-card">
            <!-- Logo ja pealkiri -->
            <div class="auth-header">
                <div class="auth-logo">
                    <?php if ($step === 'success'): ?>
                        <i class="icon-check-circle" style="color: var(--success-color);"></i>
                    <?php elseif ($step === 'error'): ?>
                        <i class="icon-alert-circle" style="color: var(--danger-color);"></i>
                    <?php else: ?>
                        <i class="icon-loader animate-spin" style="color: var(--primary-color);"></i>
                    <?php endif; ?>
                    <h1>Estreka Ratsaklubi</h1>
                </div>
                
                <?php if ($step === 'success'): ?>
                    <h2 class="auth-title">E-mail kinnitatud!</h2>
                    <p class="auth-subtitle">
                        Sinu konto on nüüd aktiivne ja saad sisse logida.
                    </p>
                <?php elseif ($step === 'error'): ?>
                    <h2 class="auth-title">Kinnitamine ebaõnnestus</h2>
                    <p class="auth-subtitle">
                        E-maili kinnitamisel tekkis probleem.
                    </p>
                <?php else: ?>
                    <h2 class="auth-title">Kinnitame sinu e-maili...</h2>
                    <p class="auth-subtitle">
                        Palun oota hetk.
                    </p>
                <?php endif; ?>
            </div>

            <!-- Sisu -->
            <div class="auth-form-container">
                <?php if ($error): ?>
                    <div class="alert alert-error">
                        <i class="icon-alert-circle"></i>
                        <span><?php echo htmlspecialchars($error); ?></span>
                    </div>
                <?php endif; ?>

                <?php if ($success): ?>
                    <div class="alert alert-success">
                        <i class="icon-check-circle"></i>
                        <span><?php echo htmlspecialchars($success); ?></span>
                    </div>
                <?php endif; ?>

                <?php if ($step === 'success'): ?>
                    <!-- Edukas kinnitamine -->
                    <div class="verification-success">
                        <div class="success-content">
                            <h3>Tere tulemast, <?php echo htmlspecialchars($user['firstName']); ?>!</h3>
                            <p>Sinu e-maili aadress <strong><?php echo htmlspecialchars($user['email']); ?></strong> on edukalt kinnitatud.</p>
                            
                            <div class="next-steps">
                                <h4>Järgmised sammud:</h4>
                                <ol>
                                    <li>Logi sisse oma kasutajanime ja parooliga</li>
                                    <li>Täida oma profiil vajadusel täiendavate andmetega</li>
                                    <li>Hakka jälgima oma võistluste tulemusi</li>
                                </ol>
                            </div>
                        </div>
                        
                        <div class="success-actions">
                            <a href="<?php echo url('login.php'); ?>" class="btn btn-primary btn-large">
                                <i class="icon-log-in"></i>
                                <span>Logi sisse</span>
                            </a>
                            <a href="<?php echo url(); ?>" class="btn btn-outline">
                                <i class="icon-home"></i>
                                <span>Avalehele</span>
                            </a>
                        </div>
                    </div>
                    
                <?php elseif ($step === 'error'): ?>
                    <!-- Viga kinnitamisel -->
                    <div class="verification-error">
                        <div class="error-content">
                            <h3>Mida saad teha?</h3>
                            
                            <div class="error-solutions">
                                <div class="solution-item">
                                    <div class="solution-icon">
                                        <i class="icon-user"></i>
                                    </div>
                                    <div class="solution-text">
                                        <h4>Registreeru uuesti</h4>
                                        <p>Kui link on aegunud, võid luua uue konto.</p>
                                        <a href="<?php echo url('register.php'); ?>" class="solution-link">
                                            Registreeru →
                                        </a>
                                    </div>
                                </div>
                                
                                <div class="solution-item">
                                    <div class="solution-icon">
                                        <i class="icon-log-in"></i>
                                    </div>
                                    <div class="solution-text">
                                        <h4>Proovi sisse logida</h4>
                                        <p>Võimalik, et konto on juba kinnitatud.</p>
                                        <a href="<?php echo url('login.php'); ?>" class="solution-link">
                                            Logi sisse →
                                        </a>
                                    </div>
                                </div>
                                
                                <div class="solution-item">
                                    <div class="solution-icon">
                                        <i class="icon-mail"></i>
                                    </div>
                                    <div class="solution-text">
                                        <h4>Võta ühendust</h4>
                                        <p>Kui probleem püsib, kirjuta meile.</p>
                                        <a href="mailto:<?php echo ADMIN_EMAIL; ?>" class="solution-link">
                                            <?php echo ADMIN_EMAIL; ?> →
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                <?php else: ?>
                    <!-- Töötlemine -->
                    <div class="verification-processing">
                        <div class="processing-spinner">
                            <i class="icon-loader animate-spin"></i>
                        </div>
                        <p>Kinnitame sinu e-maili aadressi...</p>
                        <p class="processing-note">Kui see leht ei värskenda automaatselt, <a href="javascript:location.reload()">kliki siia</a>.</p>
                    </div>
                    
                    <script>
                        // Auto-reload after 2 seconds if still processing
                        setTimeout(function() {
                            if (window.location.href.indexOf('verify-email.php') !== -1) {
                                location.reload();
                            }
                        }, 2000);
                    </script>
                <?php endif; ?>
            </div>
        </div>

        <!-- Abi info -->
        <div class="auth-info">
            <div class="info-card">
                <div class="info-icon">
                    <i class="icon-info"></i>
                </div>
                <h3>Kas teadsid?</h3>
                <ul>
                    <li>Kinnituslink kehtib 24 tundi</li>
                    <li>Iga kasutaja saab ainult ühe aktiivse konto</li>
                    <li>E-maili kinnitamine on turvalisuse mõttes kohustuslik</li>
                    <li>Pärast kinnitamist saad kohe süsteemi kasutada</li>
                </ul>
            </div>
        </div>
    </div>
</main>

<style>
/* 🎨 TÄIELIK VERIFY-EMAIL.PHP CSS (PARANDATUD INFO SEKTSIOONI) */

/* Email Verification Page Base */
.verify-email-page {
    background: linear-gradient(135deg, var(--gray-50) 0%, var(--gray-100) 100%);
    min-height: 100vh;
}

.verify-email-page .auth-main {
    padding: var(--spacing-6) 0;
    min-height: 100vh;
    display: flex;
    align-items: center;
}

.verify-email-page .auth-container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 var(--spacing-4);
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: var(--spacing-8);
    align-items: start;
}

/* Auth Card */
.verify-email-page .auth-card {
    background: white;
    border-radius: var(--radius-2xl);
    box-shadow: var(--shadow-xl);
    padding: var(--spacing-8);
    border: 1px solid var(--gray-200);
}

/* Header Styles */
.verify-email-page .auth-header {
    text-align: center;
    margin-bottom: var(--spacing-8);
}

.verify-email-page .auth-logo {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-3);
    margin-bottom: var(--spacing-6);
}

.verify-email-page .auth-logo i {
    font-size: 3rem;
    padding: var(--spacing-4);
    border-radius: var(--radius-full);
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: var(--spacing-4);
}

/* Success icon */
.verify-email-page .auth-logo i[style*="success-color"] {
    background: linear-gradient(135deg, var(--success-color), #059669);
    color: white;
    animation: successPulse 2s ease-in-out infinite;
}

/* Error icon */
.verify-email-page .auth-logo i[style*="danger-color"] {
    background: linear-gradient(135deg, var(--danger-color), #dc2626);
    color: white;
}

/* Loading icon */
.verify-email-page .auth-logo i[style*="primary-color"] {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: white;
}

@keyframes successPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.verify-email-page .auth-logo h1 {
    font-size: var(--font-size-2xl);
    font-weight: 700;
    color: var(--gray-900);
    margin: 0;
}

.verify-email-page .auth-title {
    font-size: var(--font-size-xl);
    font-weight: 600;
    margin-bottom: var(--spacing-3);
    color: var(--gray-900);
}

.verify-email-page .auth-subtitle {
    color: var(--gray-600);
    margin: 0;
    line-height: 1.6;
    font-size: var(--font-size-lg);
}

/* Verification Content */
.verification-success,
.verification-error,
.verification-processing {
    text-align: center;
    padding: var(--spacing-6) 0;
}

/* Success Content */
.success-content h3 {
    color: var(--gray-900);
    margin-bottom: var(--spacing-4);
    font-size: var(--font-size-xl);
    font-weight: 600;
}

.success-content p {
    color: var(--gray-700);
    margin-bottom: var(--spacing-6);
    line-height: 1.6;
    font-size: var(--font-size-base);
}

.next-steps {
    text-align: left;
    background: var(--gray-50);
    padding: var(--spacing-5);
    border-radius: var(--radius-lg);
    margin: var(--spacing-6) 0;
    border: 1px solid var(--gray-200);
}

.next-steps h4 {
    color: var(--gray-900);
    margin-bottom: var(--spacing-3);
    font-size: var(--font-size-lg);
    font-weight: 600;
}

.next-steps ol {
    color: var(--gray-700);
    padding-left: var(--spacing-5);
    margin: 0;
}

.next-steps li {
    margin-bottom: var(--spacing-2);
    line-height: 1.5;
}

/* Success Actions - PARANDATUD NUPUD */
.success-actions {
    display: flex;
    gap: var(--spacing-4);
    justify-content: center;
    flex-wrap: wrap;
    margin-top: var(--spacing-8);
}

.success-actions .btn {
    padding: var(--spacing-4) var(--spacing-6);
    border-radius: var(--radius-lg);
    font-weight: 600;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: var(--spacing-2);
    transition: all var(--transition-fast);
    border: 2px solid transparent;
    font-size: var(--font-size-base);
    min-width: 160px;
    justify-content: center;
}

/* Primary Button - PARANDATUD */
.success-actions .btn-primary {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
    box-shadow: var(--shadow-md);
}

.success-actions .btn-primary:hover {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

/* Outline Button - PARANDATUD */
.success-actions .btn-outline {
    background: white;
    color: var(--primary-color);
    border-color: var(--primary-color);
    box-shadow: var(--shadow-sm);
}

.success-actions .btn-outline:hover {
    background: var(--primary-color);
    color: white;
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

/* Error Content */
.error-content h3 {
    color: var(--gray-900);
    margin-bottom: var(--spacing-6);
    font-size: var(--font-size-xl);
    font-weight: 600;
}

.error-solutions {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-4);
    margin-bottom: var(--spacing-6);
}

.solution-item {
    display: flex;
    align-items: flex-start;
    gap: var(--spacing-4);
    padding: var(--spacing-4);
    background: var(--gray-50);
    border-radius: var(--radius-lg);
    text-align: left;
    border: 1px solid var(--gray-200);
    transition: all var(--transition-fast);
}

.solution-item:hover {
    background: var(--gray-100);
    border-color: var(--gray-300);
}

.solution-icon {
    width: 48px;
    height: 48px;
    background: var(--primary-color);
    color: white;
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

.solution-text h4 {
    color: var(--gray-900);
    margin-bottom: var(--spacing-2);
    font-size: var(--font-size-lg);
    font-weight: 600;
}

.solution-text p {
    color: var(--gray-600);
    font-size: var(--font-size-sm);
    margin-bottom: var(--spacing-3);
    line-height: 1.5;
}

.solution-link {
    color: var(--primary-color);
    font-weight: 600;
    font-size: var(--font-size-sm);
    text-decoration: none;
    transition: color var(--transition-fast);
}

.solution-link:hover {
    color: var(--primary-dark);
    text-decoration: underline;
}

/* Processing Styles */
.processing-spinner {
    font-size: 3rem;
    color: var(--primary-color);
    margin-bottom: var(--spacing-4);
}

.verification-processing p {
    color: var(--gray-700);
    margin-bottom: var(--spacing-3);
    font-size: var(--font-size-lg);
}

.processing-note {
    font-size: var(--font-size-sm);
    color: var(--gray-500);
}

.processing-note a {
    color: var(--primary-color);
    font-weight: 500;
    text-decoration: none;
}

.processing-note a:hover {
    text-decoration: underline;
}

/* Loading Animation */
.animate-spin {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* 🔧 PARANDATUD INFO SECTION - LOETAV TEKST */
.verify-email-page .auth-info {
    background: white;
    color: var(--gray-900);
    border-radius: var(--radius-xl);
    padding: var(--spacing-6);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--gray-200);
}

.verify-email-page .info-card {
    height: 100%;
}

.verify-email-page .info-icon {
    width: 64px;
    height: 64px;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: white;
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    margin-bottom: var(--spacing-4);
}

.verify-email-page .auth-info h3 {
    margin-bottom: var(--spacing-4);
    font-size: var(--font-size-xl);
    font-weight: 600;
    color: var(--gray-900);
}

.verify-email-page .auth-info ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

.verify-email-page .auth-info li {
    position: relative;
    padding-left: var(--spacing-4);
    margin-bottom: var(--spacing-3);
    line-height: 1.5;
    color: var(--gray-700);
    font-size: var(--font-size-sm);
}

.verify-email-page .auth-info li::before {
    content: '✓';
    position: absolute;
    left: 0;
    color: var(--success-color);
    font-weight: bold;
    font-size: var(--font-size-sm);
}

/* Alerts */
.verify-email-page .alert {
    padding: var(--spacing-4);
    border-radius: var(--radius-lg);
    margin-bottom: var(--spacing-4);
    display: flex;
    align-items: center;
    gap: var(--spacing-3);
}

.verify-email-page .alert-error {
    background-color: color-mix(in srgb, var(--danger-color) 10%, transparent);
    color: var(--danger-color);
    border: 1px solid color-mix(in srgb, var(--danger-color) 20%, transparent);
}

.verify-email-page .alert-success {
    background-color: color-mix(in srgb, var(--success-color) 10%, transparent);
    color: var(--success-color);
    border: 1px solid color-mix(in srgb, var(--success-color) 20%, transparent);
}

/* 📱 RESPONSIVE DESIGN */
@media (max-width: 1024px) {
    .verify-email-page .auth-container {
        grid-template-columns: 1fr;
        max-width: 600px;
        gap: var(--spacing-6);
    }
    
    .verify-email-page .auth-info {
        order: -1;
    }
}

@media (max-width: 768px) {
    .verify-email-page .auth-main {
        padding: var(--spacing-4) 0;
    }
    
    .verify-email-page .auth-card {
        padding: var(--spacing-6);
    }
    
    .verify-email-page .auth-info {
        padding: var(--spacing-5);
    }
    
    .success-actions {
        flex-direction: column;
    }
    
    .success-actions .btn {
        width: 100%;
    }
    
    .solution-item {
        flex-direction: column;
        text-align: center;
    }
    
    .solution-text {
        text-align: center;
    }
}

@media (max-width: 480px) {
    .verify-email-page .auth-logo i {
        width: 64px;
        height: 64px;
        font-size: 2rem;
    }
    
    .verify-email-page .auth-logo h1 {
        font-size: var(--font-size-xl);
    }
    
    .verify-email-page .auth-title {
        font-size: var(--font-size-lg);
    }
    
    .verify-email-page .auth-subtitle {
        font-size: var(--font-size-base);
    }
    
    .success-actions .btn {
        min-width: auto;
        padding: var(--spacing-3) var(--spacing-4);
    }
}
</style>

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