<?php
require_once __DIR__ . '/../includes/lang.php';
require_once __DIR__ . '/../config/db.php';
session_start();
if (isset($_SESSION['user_id'])) { header('Location: ../dashboard.php'); exit; }
$dir  = t('dir');
$code = t('lang_code');
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $email    = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm  = $_POST['confirm'] ?? '';
    $terms    = isset($_POST['terms']);

    if (!$username || !$email || !$password || !$confirm) {
        $error = t('err_required');
    } elseif ($password !== $confirm) {
        $error = t('err_pass_match');
    } elseif (!$terms) {
        $error = t('err_terms');
    } elseif (mb_strlen($password) < 6) {
        $error = 'كلمة المرور قصيرة جداً (٦ أحرف على الأقل).';
    } else {
        // Check duplicates
        $ch = db()->prepare("SELECT id FROM users WHERE username = ? OR email = ? LIMIT 1");
        $ch->execute([$username, $email]);
        $existing = $ch->fetch();
        if ($existing) {
            $error = t('err_username_taken');
        } else {
            $hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
            $ins  = db()->prepare("INSERT INTO users (username, email, password_hash, role) VALUES (?,?,?,'tourist')");
            $ins->execute([$username, $email, $hash]);
            $id = db()->lastInsertId();
            session_regenerate_id(true);
            $_SESSION['user_id']  = $id;
            $_SESSION['username'] = $username;
            $_SESSION['role']     = 'tourist';
            header('Location: ../dashboard.php');
            exit;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="<?= htmlspecialchars($code) ?>" dir="<?= htmlspecialchars($dir) ?>">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= htmlspecialchars(t('reg_tourist')) ?> — <?= htmlspecialchars(t('site_name')) ?></title>
  <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🌴</text></svg>">
  <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="../css/style.css">
  <style>
    body { background: var(--cream); }
    .auth-card { animation: fadeUp .45s ease both; }
    @keyframes fadeUp { from { opacity:0; transform:translateY(24px); } to { opacity:1; transform:none; } }
    .tourist-icon {
      width: 64px; height: 64px; border-radius: 50%;
      background: var(--g-pale); display: flex; align-items: center;
      justify-content: center; font-size: 1.8rem; margin: 0 auto 16px;
    }
  </style>
</head>
<body>

<div class="auth-wrap">
  <div class="auth-card">

    <div class="tourist-icon">👤</div>
    <h1 class="auth-title"><?= htmlspecialchars(t('reg_tourist')) ?></h1>
    <p class="auth-sub"><?= htmlspecialchars(t('reg_tourist_sub')) ?></p>

    <?php if ($error): ?>
      <div class="error-box">⚠️ <?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" novalidate>
      <div class="form-group">
        <input type="text" name="username" class="form-input <?= $error ? 'error':'' ?>"
               placeholder="<?= htmlspecialchars(t('username')) ?>"
               value="<?= htmlspecialchars($_POST['username'] ?? '') ?>"
               autocomplete="username" required autofocus>
      </div>
      <div class="form-group">
        <input type="password" name="password" class="form-input"
               placeholder="<?= htmlspecialchars(t('password')) ?>"
               autocomplete="new-password" required>
      </div>
      <div class="form-group">
        <input type="password" name="confirm" class="form-input"
               placeholder="<?= htmlspecialchars(t('confirm_pass')) ?>"
               autocomplete="new-password" required>
      </div>
      <div class="form-group">
        <input type="email" name="email" class="form-input"
               placeholder="<?= htmlspecialchars(t('email')) ?>"
               value="<?= htmlspecialchars($_POST['email'] ?? '') ?>"
               autocomplete="email" required>
      </div>

      <div class="terms-wrap">
        <input type="checkbox" name="terms" id="terms" <?= isset($_POST['terms'])?'checked':'' ?>>
        <label for="terms" class="terms-text">
          <strong><?= htmlspecialchars(t('terms_agree')) ?></strong><br>
          <?= htmlspecialchars(t('terms_detail')) ?>
        </label>
      </div>

      <button type="submit" class="btn btn-green"><?= htmlspecialchars(t('register_btn')) ?></button>
    </form>

    <div style="text-align:center;margin-top:14px">
      <a href="register.php<?= $code!=='ar'?'?lang='.$code:'' ?>" class="btn-ghost" style="font-size:.88rem;color:var(--muted);font-family:var(--font)">
        ← <?= htmlspecialchars(t('back')) ?>
      </a>
    </div>

  </div>
</div>

</body>
</html>
