<?php
// ✅ Enable error reporting during development
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once 'db.php';

// ✅ Fetch Branding Details
$stmt = $pdo->query("SELECT * FROM branding WHERE id = 1");
$branding = $stmt->fetch(PDO::FETCH_ASSOC);

$brandName = $branding['brand_name'] ?? 'Marketing System';
$logoImage = $branding['logo_image'] ?? '';
$backgroundImage = $branding['background_image'] ?? '';
$footerNote = $branding['footer_note'] ?? '';
$facebookGroup = $branding['facebook_group_url'] ?? '';

// ✅ Generate Open Graph Image Path (relative, subdirectory-safe)
$rawImage = $branding['open_graph_image'] ?? '';
$og_image = !empty($rawImage) ? ltrim($rawImage, '/') : '';

$domainName = str_replace(['https://', 'http://', 'www.'], '', $_SERVER['HTTP_HOST']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to <?= htmlspecialchars($brandName); ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600&display=swap" rel="stylesheet">
    <style>
        * {
            box-sizing: border-box;
        }
        body {
            margin: 0;
            font-family: 'Montserrat', sans-serif;
            <?php if (!empty($backgroundImage)): ?>
            background: url('<?= htmlspecialchars($backgroundImage); ?>') no-repeat center center fixed;
            background-size: cover;
            <?php else: ?>
            background: #1e1e1e;
            <?php endif; ?>
            color: #ffffff;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 2rem;
        }
        .container {
            background: rgba(0, 0, 0, 0.75);
            padding: 40px;
            border-radius: 16px;
            max-width: 720px;
            width: 100%;
            text-align: center;
            box-shadow: 0 0 25px rgba(0,0,0,0.5);
        }
        img.logo {
            max-height: 80px;
            margin-bottom: 1.5rem;
        }
        img.banner {
            max-width: 100%;
            border-radius: 12px;
            margin-bottom: 1.5rem;
            box-shadow: 0 4px 20px rgba(0,0,0,0.3);
        }
        h1 {
            font-size: 2.25rem;
            color: #00ffd5;
            margin-bottom: 1rem;
        }
        p {
            font-size: 1.1rem;
            line-height: 1.6;
            margin-bottom: 2rem;
        }
        .button {
            display: inline-block;
            background: #00ffd5;
            color: #000;
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            margin: 0.5rem;
            transition: background 0.3s ease;
        }
        .button:hover {
            background: #00c9a7;
        }
        footer {
            margin-top: 3rem;
            font-size: 0.9rem;
            color: #ccc;
        }
    </style>
</head>
<body>
    <div class="container">
        <?php if (!empty($logoImage)): ?>
            <img class="logo" src="<?= htmlspecialchars($logoImage); ?>" alt="Logo">
        <?php endif; ?>

        <?php if (!empty($og_image)): ?>
            <img class="banner" src="<?= htmlspecialchars($og_image); ?>" alt="Welcome Banner">
        <?php endif; ?>

        <h1>Welcome to <?= htmlspecialchars($brandName); ?></h1>
        <p>This powerful marketing system is designed to help you generate leads, grow your business, and promote your favorite programs — all in one place.</p>

        <a class="button" href="user-login.php">Log In to Your Account</a>

        <?php if (!empty($facebookGroup)): ?>
            <a class="button" href="<?= htmlspecialchars($facebookGroup); ?>" target="_blank">Join Our Facebook Group</a>
        <?php endif; ?>

<?php include 'footer.php'; ?>

    </div>
</body>
</html>
