<?php
require_once 'db.php';
session_start();

if (!isset($_SESSION['logged_in_user']) || !isset($_POST['page_id'])) {
    header("Location: lead-capture.php");
    exit;
}

$loggedInUser = $_SESSION['logged_in_user'];
$page_id = intval($_POST['page_id']);

// ✅ Fetch the numeric user ID
$userQuery = $pdo->prepare("SELECT id FROM users WHERE user_id = ?");
$userQuery->execute([$loggedInUser]);
$userData = $userQuery->fetch(PDO::FETCH_ASSOC);

if (!$userData) {
    die("Error: User not found.");
}

$user_id = $userData['id'];

// ✅ Reset all lead capture pages to NOT default
$pdo->prepare("UPDATE lead_capture_pages SET default_capture = 0 WHERE user_id = ?")->execute([$user_id]);

// ✅ Set selected page as default
$pdo->prepare("UPDATE lead_capture_pages SET default_capture = 1 WHERE id = ? AND user_id = ?")->execute([$page_id, $user_id]);

// ✅ Redirect back
header("Location: lead-capture.php?default_set=1");
exit;
