<?php
require_once __DIR__ . '/../includes/bootstrap.php';

if (auth()) redirect(APP_URL . '/dashboard.php');

$error = '';
$tab   = $_GET['tab'] ?? 'login';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? 'login';

    if ($action === 'login') {
        $email = trim($_POST['email'] ?? '');
        $pass  = $_POST['password']   ?? '';
        $user  = fetchOne("SELECT * FROM users WHERE email = ?", [$email]);
        if ($user && password_verify($pass, $user['password'])) {
            login($user);
            redirect(APP_URL . '/dashboard.php');
        }
        $error = 'Invalid email or password.';
    } elseif ($action === 'register') {
        $name    = trim($_POST['name']    ?? '');
        $email   = trim($_POST['email']   ?? '');
        $pass    = $_POST['password']     ?? '';
        $company = trim($_POST['company'] ?? '');
        if (!$name || !$email || !$pass) {
            $error = 'All fields required.';
        } elseif (fetchOne("SELECT id FROM users WHERE email = ?", [$email])) {
            $error = 'Email already registered.';
        } else {
            $uid  = insert('users', [
                'name'     => $name,
                'email'    => $email,
                'password' => password_hash($pass, PASSWORD_BCRYPT),
                'company'  => $company,
            ]);
            $user = fetchOne("SELECT * FROM users WHERE id = ?", [$uid]);
            login($user);
            redirect(APP_URL . '/dashboard.php');
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Debrieft — <?= $tab === 'login' ? 'Sign In' : 'Create Account' ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Syne:wght@700;800&family=DM+Sans:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="<?= APP_URL ?>/assets/css/app.css">
<script>(function(){ var t=localStorage.getItem('debrieft_theme'); if(t==='light') document.documentElement.dataset.theme='light'; })();</script>
<script>document.addEventListener('DOMContentLoaded',function(){ if(document.documentElement.dataset.theme==='light') document.body.classList.add('light'); });</script>
</head>
<body>
<div class="auth-wrap">
  <div class="auth-card">
    <div class="auth-logo">
      <img src="https://rakibhasaan.com/wp-content/uploads/2024/09/Rakib-hasan-logo-e1726952149161.webp"
           alt="Logo"
           onerror="this.outerHTML='<span class=\'brand-mark\' style=\'width:32px;height:32px;background:var(--accent);border-radius:8px;display:flex;align-items:center;justify-content:center;font-family:Syne,sans-serif;font-weight:800;font-size:16px;color:#fff\'>D</span>'">
    </div>

    <?php if ($error): ?>
      <div class="flash flash-error"><?= e($error) ?></div>
    <?php endif; ?>

    <?php if ($tab === 'login'): ?>
      <h2>Welcome back</h2>
      <p>Sign in to your designer workspace.</p>
      <form method="POST">
        <input type="hidden" name="action" value="login">
        <div class="form-group">
          <label>Email</label>
          <input type="email" name="email" required placeholder="you@studio.com" autocomplete="email">
        </div>
        <div class="form-group">
          <label>Password</label>
          <input type="password" name="password" required placeholder="••••••••" autocomplete="current-password">
        </div>
        <button class="btn btn-primary" style="width:100%;justify-content:center" type="submit">Sign In</button>
      </form>
      <p style="margin-top:20px;text-align:center;font-size:13px;color:var(--text2)">
        No account? <a href="?tab=register" style="color:var(--accent2)">Create one</a>
      </p>

    <?php else: ?>
      <h2>Create your workspace</h2>
      <p>Start managing your design projects today.</p>
      <form method="POST">
        <input type="hidden" name="action" value="register">
        <div class="form-group">
          <label>Full Name</label>
          <input type="text" name="name" required placeholder="Jane Smith">
        </div>
        <div class="form-group">
          <label>Studio / Company</label>
          <input type="text" name="company" placeholder="Smith Studio">
        </div>
        <div class="form-group">
          <label>Email</label>
          <input type="email" name="email" required placeholder="jane@smithstudio.com">
        </div>
        <div class="form-group">
          <label>Password</label>
          <input type="password" name="password" required placeholder="Min 8 characters" minlength="8">
        </div>
        <button class="btn btn-primary" style="width:100%;justify-content:center" type="submit">Create Account</button>
      </form>
      <p style="margin-top:20px;text-align:center;font-size:13px;color:var(--text2)">
        Already have an account? <a href="?tab=login" style="color:var(--accent2)">Sign in</a>
      </p>
    <?php endif; ?>
  </div>
</div>
</body>
</html>
