<?php
require_once '../config/db.php';
require_once '../config/auth.php';

if (session_status() === PHP_SESSION_NONE) session_start();

// Must come from Google OAuth flow
if (empty($_SESSION['google_pending_id'])) {
    header('Location: /lostfound_relink/auth/register.php');
    exit;
}

$msg = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $uid    = (int)$_SESSION['google_pending_id'];
    $gender = $conn->real_escape_string($_POST['gender'] ?? 'other');
    $phone  = $conn->real_escape_string(trim($_POST['phone'] ?? ''));

    $conn->query("UPDATE users SET gender='$gender', phone='$phone' WHERE id=$uid");

    unset($_SESSION['google_pending_id'], $_SESSION['google_name'], $_SESSION['google_email'], $_SESSION['google_avatar']);

    $msg = 'success';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Complete Profile - RELINK</title>
<link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body>
<div class="auth-wrapper">
    <div class="auth-card" style="max-width:440px">
        <div class="auth-logo">
            <h1>🔗 RELINK</h1>
            <?php if(!empty($_SESSION['google_avatar'])): ?>
            <img src="<?= htmlspecialchars($_SESSION['google_avatar']) ?>" alt="avatar"
                 style="width:56px;height:56px;border-radius:50%;margin:10px auto;display:block;border:3px solid var(--primary)">
            <?php endif; ?>
            <p>Welcome, <strong><?= htmlspecialchars($_SESSION['google_name'] ?? '') ?></strong>!</p>
            <p style="font-size:12px;color:#9ca3af"><?= htmlspecialchars($_SESSION['google_email'] ?? '') ?></p>
        </div>

        <?php if($msg === 'success'): ?>
        <div class="alert alert-success">
            ✅ Profile submitted! Your account is <strong>pending admin approval</strong>. You'll be able to login once activated.
        </div>
        <a href="<?= BASE_URL ?>/auth/login.php" class="btn btn-primary btn-block">Go to Login</a>
        <?php else: ?>
        <div class="alert alert-info" style="margin-bottom:20px">
            📋 Almost done! Please fill in a few more details to complete your registration.
        </div>
        <?php if($error): ?><div class="alert alert-danger"><?= $error ?></div><?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label>Gender <span style="color:var(--danger)">*</span></label>
                <select name="gender" class="form-control" required>
                    <option value="">Select gender</option>
                    <option value="male">Male</option>
                    <option value="female">Female</option>
                    <option value="other">Other / Prefer not to say</option>
                </select>
            </div>
            <div class="form-group">
                <label>Phone Number <span style="color:var(--text-muted);font-weight:400">(optional)</span></label>
                <input type="text" name="phone" class="form-control" placeholder="09XXXXXXXXX">
                <small style="color:var(--text-muted);font-size:11px">Used for SMS notifications when a match is found.</small>
            </div>
            <button type="submit" class="btn btn-primary btn-block">Complete Registration</button>
        </form>
        <p style="text-align:center;margin-top:14px;font-size:12px;color:#9ca3af">
            <a href="<?= BASE_URL ?>/auth/login.php" style="color:#9ca3af">← Back to Login</a>
        </p>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
