<?php
require_once __DIR__ . '/includes/functions.php';
startSecureSession();

if (isLoggedIn()) {
    header('Location: dashboard.php');
    exit;
}
if (isAdminLoggedIn()) {
    header('Location: admin/dashboard.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $login = trim($_POST['login'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (empty($login) || empty($password)) {
        $error = 'Please fill in all fields.';
    } else {
        if ($login === ADMIN_USERNAME && $password === ADMIN_PASSWORD) {
            $_SESSION['is_admin'] = true;
            header('Location: admin/dashboard.php');
            exit;
        }
        
        $user = getUserByEmail($login);
        if (!$user) {
            $user = getUserByUsername($login);
        }
        
        if (!$user || !password_verify($password, $user['password_hash'])) {
            $error = 'Invalid username/email or password.';
        } elseif ($user['status'] === 'banned') {
            $error = 'This account has been suspended. Contact support.';
        } else {
            $_SESSION['user_id'] = $user['id'];
            header('Location: dashboard.php');
            exit;
        }
    }
}

$pageTitle = 'Sign In';
$metaDesc = 'Sign in to your FamAPI dashboard to manage API keys, payment links, and transactions.';
include __DIR__ . '/includes/public_header.php';
?>
<style>
.auth-page{min-height:100vh;display:flex;align-items:center;justify-content:center;padding:40px 20px;background:var(--soft)}
.auth-card{width:100%;max-width:420px;background:#fff;border:1px solid var(--line);border-radius:16px;box-shadow:var(--shadow-md);padding:40px 32px}
.auth-header{text-align:center;margin-bottom:32px}
.auth-logo{display:inline-flex;align-items:center;gap:10px;font-weight:800;font-size:22px;color:var(--ink);margin-bottom:16px}
.auth-logo .brand-accent{color:var(--brand)}
.auth-title{font-size:26px;font-weight:800;color:var(--ink);letter-spacing:-.5px;margin-bottom:8px}
.auth-sub{font-size:15px;color:var(--muted)}
.auth-form{display:flex;flex-direction:column;gap:20px}
.form-group{display:flex;flex-direction:column;gap:6px}
.form-label{font-size:13px;font-weight:600;color:var(--ink)}
.form-input{padding:12px 14px;border:1.5px solid var(--line);border-radius:10px;font-size:15px;font-family:inherit;color:var(--ink);background:#fff;transition:all .2s ease;outline:none}
.form-input:focus{border-color:var(--brand);box-shadow:0 0 0 3px rgba(163,112,252,.15)}
.form-input::placeholder{color:var(--muted-soft)}
.btn-submit{padding:14px 24px;font-size:15px;font-weight:700;border-radius:10px;margin-top:8px}
.auth-divider{display:flex;align-items:center;gap:12px;margin:24px 0;color:var(--muted);font-size:13px}
.auth-divider::before,.auth-divider::after{content:"";flex:1;height:1px;background:var(--line)}
.auth-footer{text-align:center;font-size:14px;color:var(--muted);margin-top:24px}
.auth-footer a{color:var(--brand);font-weight:600;text-decoration:none}
.auth-footer a:hover{text-decoration:underline}
.alert{padding:12px 14px;border-radius:10px;font-size:13.5px;font-weight:500;margin-bottom:20px}
.alert-error{background:#fef2f2;border:1px solid #fecaca;color:#dc2626}
</style>

<div class="auth-page">
  <div class="auth-card">
    <div class="auth-header">
      <a href="index.php" class="auth-logo">
        <svg width="32" height="32" viewBox="0 0 32 32" fill="none"><rect width="32" height="32" rx="8" fill="#a370fc"/><path d="M8 12l-3 3 3 3M24 12l3 3-3 3M21 8l-2 12" stroke="white" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
        Fam<span class="brand-accent">API</span>
      </a>
      <h1 class="auth-title">Welcome back</h1>
      <p class="auth-sub">Sign in to manage your API keys & payments</p>
    </div>

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

    <form method="POST" class="auth-form">
      <div class="form-group">
        <label class="form-label" for="login">Username or Email</label>
        <input class="form-input" type="text" id="login" name="login" placeholder="Enter username or email" required autofocus autocomplete="username">
      </div>

      <div class="form-group">
        <label class="form-label" for="password">Password</label>
        <input class="form-input" type="password" id="password" name="password" placeholder="Enter your password" required autocomplete="current-password">
      </div>

      <button type="submit" class="btn btn-primary btn-block btn-submit">Sign In</button>
    </form>

    <div class="auth-divider">or</div>

    <div class="auth-footer">
      <a href="forgot-password.php">Forgot password?</a>
    </div>
    <div class="auth-footer" style="margin-top:12px;">
      Don't have an account? <a href="register.php">Create one free</a>
    </div>
  </div>
</div>

<?php include __DIR__ . '/includes/public_footer.php'; ?>