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

billing_require_auth();

$user = billing_current_user();
$organization = billing_current_organization();

if (!$user) {
    billing_redirect('signin.php');
}

if ($organization && !empty($organization['onboarding_completed_at'])) {
    billing_redirect('dashboard.php?tab=dashboard');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $organization) {
    $conn = billing_db();
    $stmt = $conn->prepare('UPDATE organizations SET onboarding_completed_at = NOW() WHERE id = ? LIMIT 1');
    if ($stmt) {
        $orgId = (int) $organization['id'];
        $stmt->bind_param('i', $orgId);
        $stmt->execute();
        $stmt->close();
    }
    billing_redirect('dashboard.php?tab=dashboard');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title>Onboarding</title>
<link rel="stylesheet" href="assets/billing.css">
</head>
<body class="billing-auth-body">
<main class="auth-shell">
    <section class="auth-panel" style="max-width:720px;margin:48px auto;">
        <div class="auth-header">
            <h1>Onboarding</h1>
            <p>Your organization setup needs to be completed once before opening the dashboard.</p>
        </div>
        <form method="post">
            <button class="auth-submit" type="submit">Complete Setup</button>
        </form>
    </section>
</main>
</body>
</html>

