<?php
// profile-setup.php — Dabil "Complete Your Profile" screen
// Shown after OTP verification during signup.
// Submits to complete-profile.php (full name, phone, password).

session_start();

// If already signed in, skip straight to the dashboard.
if (isset($_SESSION['user_id']) && ($_SESSION['role'] ?? '') === 'customer') {
    header('Location: dashboard.php');
    exit;
}

// The email verified via OTP is carried over in the session by
// verify-otp.php once verification succeeds. If it's missing, the user
// hasn't completed (or has lost) the verification step — send them back.
if (empty($_SESSION['signup_email']) || empty($_SESSION['signup_email_verified'])) {
    header('Location: signup-email.php');
    exit;
}

$verifiedEmail = $_SESSION['signup_email'];

$profileError = $_SESSION['profile_error'] ?? '';
unset($_SESSION['profile_error']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<title>Profile Set up — Dabil</title>
<style>
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

  :root {
    --primary:      #2563EB;
    --primary-dark: #1D4ED8;
    --primary-bg:   #EFF6FF;
    --text:         #0F172A;
    --text-muted:   #64748B;
    --text-light:   #94A3B8;
    --bg:           #F4F6FB;
    --card-bg:      #FFFFFF;
    --card-border:  #E2E8F0;
    --field-border: #E5E9F0;
    --radius:       16px;
    --radius-card:  20px;
  }

  html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: var(--bg);
    overflow-x: hidden;
    color: var(--text);
  }

  .page {
    display: flex;
    flex-direction: column;
    min-height: 100%;
    max-width: 430px;
    margin: 0 auto;
    background: var(--bg);
    position: relative;
  }

  /* ── Top bar ── */
  .topbar {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: calc(env(safe-area-inset-top, 0px) + 18px) 20px 16px;
    background: #fff;
    border-bottom: 1px solid var(--card-border);
  }

  .back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    color: var(--primary);
    text-decoration: none;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    transition: background .15s;
  }

  .back-btn:active { background: var(--primary-bg); }

  .topbar h1 {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text);
  }

  /* ── Content ── */
  .content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 28px 20px;
  }

  /* ── Headline ── */
  .headline {
    padding: 4px 0 24px;
  }

  .headline h2 {
    font-size: 1.55rem;
    font-weight: 800;
    letter-spacing: -.02em;
    line-height: 1.2;
    color: var(--primary);
    margin-bottom: 8px;
  }

  .headline p {
    font-size: .9rem;
    color: var(--text-muted);
    line-height: 1.55;
  }

  /* ── Form card ── */
  .form-card {
    background: var(--card-bg);
    border-radius: var(--radius-card);
    padding: 22px 20px 8px;
    box-shadow: 0 2px 16px rgba(15,23,42,.05);
    border: 1px solid var(--card-border);
  }

  .field { margin-bottom: 22px; }

  .field label {
    display: block;
    font-size: .68rem;
    font-weight: 700;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--text-light);
    margin-bottom: 10px;
  }

  .field-input {
    display: flex;
    align-items: center;
    gap: 12px;
    padding-bottom: 10px;
    border-bottom: 1.5px solid var(--field-border);
    transition: border-color .15s;
  }

  .field-input:focus-within {
    border-color: var(--primary);
  }

  .field-input svg {
    flex-shrink: 0;
    color: var(--text-light);
  }

  .field-input:focus-within svg {
    color: var(--primary);
  }

  .field-input input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    font-size: .98rem;
    font-family: inherit;
    color: var(--text);
  }

  .field-input input::placeholder {
    color: var(--text-light);
  }

  /* ── Security notice ── */
  .secure-note {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 16px 4px 22px;
  }

  .secure-note .icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    flex-shrink: 0;
  }

  .secure-note .text strong {
    display: block;
    font-size: .9rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 2px;
  }

  .secure-note .text p {
    font-size: .8rem;
    color: var(--text-muted);
    line-height: 1.5;
  }

  /* ── Submit button ── */
  .btn-submit {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    padding: 17px;
    margin-top: 6px;
    background: var(--primary);
    color: #fff;
    font-size: 1rem;
    font-weight: 700;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(37,99,235,.32);
    transition: background .15s, transform .12s;
    -webkit-tap-highlight-color: transparent;
  }

  .btn-submit:active {
    background: var(--primary-dark);
    transform: scale(.98);
  }

  /* ── Error message ── */
  .form-error {
    background: #FEF2F2;
    border: 1px solid #FECACA;
    color: #B91C1C;
    font-size: .85rem;
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 18px;
    text-align: center;
  }

  .footer-space {
    height: calc(env(safe-area-inset-bottom, 0px) + 24px);
  }

  /* ── Animations ── */
  .anim-up {
    animation: slideUp .5s cubic-bezier(.22,1,.36,1) both;
  }
  .anim-up.d1 { animation-delay: .05s; }
  .anim-up.d2 { animation-delay: .12s; }
  .anim-up.d3 { animation-delay: .19s; }

  @keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
  }

  @media (prefers-reduced-motion: reduce) {
    *, .anim-up { animation: none !important; }
  }
</style>
</head>
<body>

<div class="page">

  <!-- ── Top bar ── -->
  <div class="topbar">
    <a href="javascript:history.back()" class="back-btn" aria-label="Go back">
      <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round">
        <polyline points="15 18 9 12 15 6"/>
      </svg>
    </a>
    <h1>Profile Set up</h1>
  </div>

  <!-- ── Content ── -->
  <div class="content">

    <!-- Headline -->
    <div class="headline anim-up d1">
      <h2>Complete Your Profile</h2>
      <p>Just a few more details to personalize your Dabil experience.</p>
    </div>

    <?php if ($profileError): ?>
      <div class="form-error anim-up d1"><?php echo htmlspecialchars($profileError, ENT_QUOTES, 'UTF-8'); ?></div>
    <?php endif; ?>

    <!-- Form -->
    <form action="complete-profile.php" method="POST" class="anim-up d2" autocomplete="off">
      <input type="hidden" name="email" value="<?php echo htmlspecialchars($verifiedEmail, ENT_QUOTES, 'UTF-8'); ?>">

      <div class="form-card">

        <!-- Full Name -->
        <div class="field">
          <label for="full_name">Full Name</label>
          <div class="field-input">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/>
              <circle cx="12" cy="7" r="4"/>
            </svg>
            <input type="text" id="full_name" name="full_name" placeholder="Enter your full name" required>
          </div>
        </div>

        <!-- Phone Number -->
        <div class="field">
          <label for="phone">Phone Number</label>
          <div class="field-input">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.127.96.362 1.903.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.907.338 1.85.573 2.81.7A2 2 0 0122 16.92z"/>
            </svg>
            <input type="tel" id="phone" name="phone" placeholder="+234" required>
          </div>
        </div>

        <!-- Password -->
        <div class="field">
          <label for="password">Password</label>
          <div class="field-input">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <rect x="3" y="11" width="18" height="11" rx="2"/>
              <path d="M7 11V7a5 5 0 0110 0v4"/>
            </svg>
            <input type="password" id="password" name="password" placeholder="••••••••" minlength="8" required>
          </div>
        </div>

      </div><!-- /.form-card -->

      <!-- Secure & Private notice -->
      <div class="secure-note anim-up d3">
        <div class="icon">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round">
            <path d="M12 2l8 4v6c0 5-3.5 8-8 10-4.5-2-8-5-8-10V6l8-4z"/>
            <path d="M9 12l2 2 4-4"/>
          </svg>
        </div>
        <div class="text">
          <strong>Secure &amp; Private</strong>
          <p>Your data is encrypted with bank-level security.</p>
        </div>
      </div>

      <!-- Submit -->
      <button type="submit" class="btn-submit anim-up d3">
        Complete Setup
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
          <line x1="5" y1="12" x2="19" y2="12"/>
          <polyline points="12 5 19 12 12 19"/>
        </svg>
      </button>

      <div class="footer-space"></div>
    </form>

  </div><!-- /.content -->

</div><!-- /.page -->

</body>
</html>
