<?php
declare(strict_types=1);
require_once __DIR__ . '/functions.php';

// Enhanced header with SEO and branding support
$pdo = require __DIR__ . '/../config/database.php';

// Get all settings in one query
$settings = [];
$stmt = $pdo->query("SELECT key, value FROM settings");
while ($row = $stmt->fetch()) {
    $settings[$row['key']] = $row['value'];
}

// Site branding
$siteName = $settings['site_name'] ?? 'Go Legal';
$siteLogo = $settings['site_logo'] ?? '';
$siteFavicon = $settings['site_favicon'] ?? '';
$siteTagline = $settings['site_tagline'] ?? 'Professional Business Registration Services';

// SEO settings
$metaKeywords = $settings['meta_keywords'] ?? '';
$metaDescription = $settings['meta_description'] ?? $siteTagline;
$ogImage = $settings['og_image'] ?? '';
$twitterHandle = $settings['twitter_handle'] ?? '';
$googleVerification = $settings['google_site_verification'] ?? '';
$bingVerification = $settings['bing_site_verification'] ?? '';

// Override meta if provided in page
$meta = $meta ?? [];
$title = $meta['title'] ?? $siteName;
$description = $meta['description'] ?? $metaDescription;
$keywords = $meta['keywords'] ?? $metaKeywords;
$canonical = $meta['canonical'] ?? '';

// Output SEO meta tags
if (!empty($keywords)) {
    echo "<meta name=\"keywords\" content=\"" . e($keywords) . "\">\n";
}

if (!empty($ogImage)) {
    echo "<meta property=\"og:image\" content=\"" . e($ogImage) . "\">\n";
    echo "<meta property=\"og:image:width\" content=\"1200\">\n";
    echo "<meta property=\"og:image:height\" content=\"630\">\n";
}

if (!empty($twitterHandle)) {
    echo "<meta name=\"twitter:card\" content=\"summary_large_image\">\n";
    echo "<meta name=\"twitter:site\" content=\"" . e($twitterHandle) . "\">\n";
}

if (!empty($googleVerification)) {
    echo "<meta name=\"google-site-verification\" content=\"" . e($googleVerification) . "\">\n";
}

if (!empty($bingVerification)) {
    echo "<meta name=\"msvalidate.01\" content=\"" . e($bingVerification) . "\">\n";
}

// Favicon
if (!empty($siteFavicon)) {
    echo "<link rel=\"icon\" type=\"image/x-icon\" href=\"" . e($siteFavicon) . "\">\n";
    echo "<link rel=\"shortcut icon\" href=\"" . e($siteFavicon) . "\">\n";
}

// Logo CSS override
if (!empty($siteLogo)) {
    echo "<style>\n";
    echo ".logo { background-image: url('" . e($siteLogo) . "'); background-size: contain; background-repeat: no-repeat; background-position: center; }\n";
    echo "</style>\n";
}
?>
