<?php
/**
 * Reset Password Page
 * Digital Marketplace
 */

require_once __DIR__ . '/config/database.php';

$page_title = 'Reset Password';

$token = sanitize($_GET['token'] ?? '');
$errors = [];
$success = false;

// Validate token
$valid_token = false;
$user_id = null;

if ($token) {
    $stmt = $pdo->prepare("SELECT user_id FROM password_resets WHERE token = ? AND expires_at > NOW() AND used = 0");
    $stmt->execute([$token]);
    $reset = $stmt->fetch();
    
    if ($reset) {
        $valid_token = true;
        $user_id = $reset['user_id'];
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $valid_token) {
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    
    if (strlen($password) < 8) {
        $errors[] = 'Password must be at least 8 characters long.';
    } elseif ($password !== $confirm_password) {
        $errors[] = 'Passwords do not match.';
    } else {
        // Update password
        $hashed_password = password_hash($password, PASSWORD_BCRYPT);
        $stmt = $pdo->prepare("UPDATE users SET password = ?, updated_at = NOW() WHERE id = ?");
        $stmt->execute([$hashed_password, $user_id]);
        
        // Mark token as used
        $stmt = $pdo->prepare("UPDATE password_resets SET used = 1 WHERE token = ?");
        $stmt->execute([$token]);
        
        $success = true;
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?> | Digital Marketplace</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
    :root {
        --primary: #6366f1;
        --primary-dark: #4f46e5;
        --dark: #0f172a;
        --dark-lighter: #1e293b;
    }
    
    body {
        font-family: 'Inter', sans-serif;
        background: var(--dark);
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 2rem;
    }
    
    .auth-card {
        background: var(--dark-lighter);
        border: 1px solid rgba(255,255,255,0.1);
        border-radius: 1rem;
        padding: 2.5rem;
        width: 100%;
        max-width: 420px;
    }
    
    .auth-logo {
        text-align: center;
        margin-bottom: 2rem;
    }
    
    .auth-logo i {
        font-size: 3rem;
        color: var(--primary);
    }
    
    .auth-logo h4 {
        color: white;
        margin-top: 1rem;
    }
    
    .form-control {
        background: var(--dark);
        border: 1px solid rgba(255,255,255,0.1);
        color: white;
        padding: 0.875rem 1rem;
    }
    
    .form-control:focus {
        background: var(--dark);
        border-color: var(--primary);
        color: white;
        box-shadow: 0 0 0 0.2rem rgba(99, 102, 241, 0.25);
    }
    
    .btn-primary {
        background: var(--primary);
        border-color: var(--primary);
        padding: 0.875rem 1.5rem;
        font-weight: 500;
    }
    
    .btn-primary:hover {
        background: var(--primary-dark);
        border-color: var(--primary-dark);
    }
    
    .text-muted {
        color: rgba(255,255,255,0.5) !important;
    }
    </style>
</head>
<body>
    <div class="auth-card">
        <div class="auth-logo">
            <i class="fas fa-store"></i>
            <h4>Digital Marketplace</h4>
        </div>
        
        <?php if (!$valid_token): ?>
            <div class="text-center">
                <i class="fas fa-exclamation-circle fa-3x text-danger mb-3"></i>
                <h5 class="text-white mb-3">Invalid or Expired Link</h5>
                <p class="text-muted mb-4">
                    This password reset link is invalid or has expired. Please request a new one.
                </p>
                <a href="<?php echo BASE_URL; ?>/forgot-password.php" class="btn btn-primary w-100">
                    <i class="fas fa-redo me-2"></i>Request New Link
                </a>
            </div>
        <?php elseif ($success): ?>
            <div class="text-center">
                <i class="fas fa-check-circle fa-3x text-success mb-3"></i>
                <h5 class="text-white mb-3">Password Reset Successful</h5>
                <p class="text-muted mb-4">
                    Your password has been reset successfully. You can now log in with your new password.
                </p>
                <a href="<?php echo BASE_URL; ?>/login.php" class="btn btn-primary w-100">
                    <i class="fas fa-sign-in-alt me-2"></i>Go to Login
                </a>
            </div>
        <?php else: ?>
            <h5 class="text-white mb-3">Reset Your Password</h5>
            <p class="text-muted mb-4">Enter your new password below.</p>
            
            <?php if (!empty($errors)): ?>
                <div class="alert alert-danger">
                    <?php foreach ($errors as $error): ?>
                        <p class="mb-1"><i class="fas fa-exclamation-circle me-2"></i><?php echo $error; ?></p>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label text-white">New Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Enter new password" minlength="8" required>
                    <small class="text-muted">Minimum 8 characters</small>
                </div>
                
                <div class="mb-4">
                    <label class="form-label text-white">Confirm Password</label>
                    <input type="password" name="confirm_password" class="form-control" placeholder="Confirm new password" minlength="8" required>
                </div>
                
                <button type="submit" class="btn btn-primary w-100">
                    <i class="fas fa-save me-2"></i>Reset Password
                </button>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
