<?php
// nyaya-guru/register.php — Client Registration
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/auth.php';

if (isUserLoggedIn()) { redirect('client/dashboard.php'); }

$error = $success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validateCSRF($_POST['csrf_token'] ?? '')) { $error = 'Invalid request.'; }
    else {
        $name     = sanitize($_POST['name'] ?? '');
        $email    = sanitizeEmail($_POST['email'] ?? '');
        $mobile   = sanitize($_POST['mobile'] ?? '');
        $password = $_POST['password'] ?? '';
        $confirm  = $_POST['confirm_password'] ?? '';

        if (!$name || !$email || !$mobile || !$password) {
            $error = 'All fields are required.';
        } elseif (!validateEmail($email)) {
            $error = 'Invalid email address.';
        } elseif (strlen($password) < 6) {
            $error = 'Password must be at least 6 characters.';
        } elseif ($password !== $confirm) {
            $error = 'Passwords do not match.';
        } else {
            $db = getDB();
            $exists = $db->prepare("SELECT id FROM users WHERE email=? OR mobile=?");
            $exists->execute([$email, $mobile]);
            if ($exists->fetch()) {
                $error = 'Email or mobile already registered.';
            } else {
                $hashed = hashPassword($password);
                $db->prepare("INSERT INTO users (name,email,mobile,password,status) VALUES (?,?,?,?,1)")
                   ->execute([$name, $email, $mobile, $hashed]);
                $userId = $db->lastInsertId();
                $user = $db->prepare("SELECT * FROM users WHERE id=?");
                $user->execute([$userId]);
                loginUser($user->fetch());
                setFlash('success', 'Welcome to MeeVakeel, '.$name.'!');
                redirect('client/dashboard.php');
            }
        }
    }
}

$pageTitle = 'Register as Client — MeeVakeel';
require_once __DIR__ . '/includes/header.php';
?>
<section style="min-height:80vh;background:var(--off-white);display:flex;align-items:center;padding:60px 0;">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">

  <div style="background:white;border-radius:20px;overflow:hidden;box-shadow:var(--shadow-lg);">
    <div style="background:linear-gradient(135deg,var(--primary),#1a3a6b);padding:32px;text-align:center;">
      <div style="font-family:var(--font-heading);font-size:1.6rem;font-weight:800;color:white;"><span style="color:var(--gold);">Mee</span> Vakeel</div>
      <p style="color:rgba(255,255,255,.7);margin:6px 0 0;font-size:.88rem;">Create your free client account</p>
    </div>
    <div style="padding:32px;">
      <?php if ($error): ?>
      <div class="alert alert-danger rounded-3 border-0" style="font-size:.88rem;"><i class="fas fa-exclamation-circle me-2"></i><?= $error ?></div>
      <?php endif; ?>

      <form method="POST">
        <input type="hidden" name="csrf_token" value="<?= generateCSRF() ?>">
        <div class="mb-3">
          <label class="form-label">Full Name <span class="text-danger">*</span></label>
          <input type="text" name="name" class="form-control" placeholder="Your full name" value="<?= htmlspecialchars($_POST['name']??'') ?>" required>
        </div>
        <div class="mb-3">
          <label class="form-label">Email Address <span class="text-danger">*</span></label>
          <input type="email" name="email" class="form-control" placeholder="you@email.com" value="<?= htmlspecialchars($_POST['email']??'') ?>" required>
        </div>
        <div class="mb-3">
          <label class="form-label">Mobile Number <span class="text-danger">*</span></label>
          <input type="tel" name="mobile" class="form-control" placeholder="10-digit mobile" maxlength="10" value="<?= htmlspecialchars($_POST['mobile']??'') ?>" required>
        </div>
        <div class="mb-3">
          <label class="form-label">Password <span class="text-danger">*</span></label>
          <input type="password" name="password" class="form-control" placeholder="Min 6 characters" required>
        </div>
        <div class="mb-4">
          <label class="form-label">Confirm Password <span class="text-danger">*</span></label>
          <input type="password" name="confirm_password" class="form-control" placeholder="Repeat password" required>
        </div>
        <button type="submit" class="btn btn-gold w-100 py-3" style="font-weight:700;border-radius:12px;">
          <i class="fas fa-user-plus me-2"></i>Create Account
        </button>
      </form>

      <div style="text-align:center;margin-top:20px;">
        <span style="color:var(--text-light);font-size:.88rem;">Already have an account?</span>
        <a href="login.php" style="color:var(--primary);font-weight:700;font-size:.88rem;margin-left:6px;">Login</a>
      </div>
      <div style="text-align:center;margin-top:8px;">
        <a href="register-lawyer.php" style="color:var(--text-light);font-size:.82rem;">Register as a Lawyer instead →</a>
      </div>
    </div>
  </div>

</div>
</div>
</div>
</section>
<?php require_once __DIR__ . '/includes/footer.php'; ?>