<?php
/**
 * META TAGS & FAVICON INCLUDE
 * ============================
 * Add this inside your <head> tag in side.php (or wherever your header is).
 * Usage: <?php include 'meta_tags.php'; ?>
 * 
 * This will make the admin-uploaded logo appear as:
 * - Browser favicon (tab icon)
 * - WhatsApp link preview image
 * - Facebook/Twitter share image
 * - Google search result icon
 * - Apple home screen icon
 * - Telegram/Discord/Slack link previews
 */

// Get settings (safe to call even if already loaded)
$metaSiteName = getSetting('site_name', 'PrimePanel');
$metaSiteDesc = getSetting('site_description', 'Earn money online');
$metaSiteLogo = getSetting('site_logo', '');
$metaThemeColor = getSetting('theme_color', '#5B77F5');

// Build full logo URL
$metaLogoPath = '';
$metaLogoFullUrl = '';

if (!empty($metaSiteLogo) && file_exists(__DIR__ . '/../uploads/logos/' . $metaSiteLogo)) {
    $metaLogoPath = '../uploads/logos/' . $metaSiteLogo;
    
    // Build absolute URL for meta tags (they require full URLs)
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
    $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
    $basePath = rtrim(dirname(dirname($_SERVER['SCRIPT_NAME'])), '/');
    $metaLogoFullUrl = $protocol . '://' . $host . $basePath . '/uploads/logos/' . $metaSiteLogo;
} elseif (defined('SITE_URL')) {
    // Fallback: use a default icon path if no logo uploaded
    $metaLogoFullUrl = SITE_URL . '/assets/images/default-logo.png';
}

// Current page URL
$currentUrl = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http') 
              . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost') . ($_SERVER['REQUEST_URI'] ?? '/');
?>

<!-- ============================================ -->
<!-- FAVICON & APP ICONS -->
<!-- ============================================ -->

<!-- Web App Manifest (REQUIRED for Chrome "Add to Home Screen" icon) -->
<link rel="manifest" href="manifest.php">

<?php if (!empty($metaLogoPath)): ?>
<!-- Standard Favicon -->
<link rel="icon" type="image/png" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="icon" type="image/x-icon" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="shortcut icon" href="<?php echo htmlspecialchars($metaLogoPath); ?>">

<!-- Apple Touch Icon (home screen bookmark) -->
<link rel="apple-touch-icon" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="apple-touch-icon" sizes="180x180" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="apple-touch-icon" sizes="152x152" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="apple-touch-icon" sizes="120x120" href="<?php echo htmlspecialchars($metaLogoPath); ?>">

<!-- Android Chrome -->
<link rel="icon" type="image/png" sizes="192x192" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="icon" type="image/png" sizes="32x32" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<link rel="icon" type="image/png" sizes="16x16" href="<?php echo htmlspecialchars($metaLogoPath); ?>">
<?php endif; ?>

<!-- Theme Color (browser address bar color on mobile) -->
<meta name="theme-color" content="<?php echo htmlspecialchars($metaThemeColor); ?>">
<meta name="msapplication-TileColor" content="<?php echo htmlspecialchars($metaThemeColor); ?>">
<?php if (!empty($metaLogoFullUrl)): ?>
<meta name="msapplication-TileImage" content="<?php echo htmlspecialchars($metaLogoFullUrl); ?>">
<?php endif; ?>

<!-- ============================================ -->
<!-- PRIMARY META TAGS -->
<!-- ============================================ -->
<meta name="title" content="<?php echo htmlspecialchars($metaSiteName); ?>">
<meta name="description" content="<?php echo htmlspecialchars($metaSiteDesc); ?>">
<meta name="application-name" content="<?php echo htmlspecialchars($metaSiteName); ?>">
<meta name="apple-mobile-web-app-title" content="<?php echo htmlspecialchars($metaSiteName); ?>">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="mobile-web-app-capable" content="yes">

<!-- ============================================ -->
<!-- OPEN GRAPH (Facebook, WhatsApp, Telegram, Discord, Slack) -->
<!-- ============================================ -->
<meta property="og:type" content="website">
<meta property="og:url" content="<?php echo htmlspecialchars($currentUrl); ?>">
<meta property="og:title" content="<?php echo htmlspecialchars($metaSiteName); ?>">
<meta property="og:description" content="<?php echo htmlspecialchars($metaSiteDesc); ?>">
<meta property="og:site_name" content="<?php echo htmlspecialchars($metaSiteName); ?>">
<?php if (!empty($metaLogoFullUrl)): ?>
<meta property="og:image" content="<?php echo htmlspecialchars($metaLogoFullUrl); ?>">
<meta property="og:image:width" content="512">
<meta property="og:image:height" content="512">
<meta property="og:image:alt" content="<?php echo htmlspecialchars($metaSiteName); ?> Logo">
<?php endif; ?>

<!-- ============================================ -->
<!-- TWITTER CARD (Twitter/X previews) -->
<!-- ============================================ -->
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="<?php echo htmlspecialchars($metaSiteName); ?>">
<meta name="twitter:description" content="<?php echo htmlspecialchars($metaSiteDesc); ?>">
<?php if (!empty($metaLogoFullUrl)): ?>
<meta name="twitter:image" content="<?php echo htmlspecialchars($metaLogoFullUrl); ?>">
<meta name="twitter:image:alt" content="<?php echo htmlspecialchars($metaSiteName); ?> Logo">
<?php endif; ?>