<?php
/**
 * SEO Meta Tags and Structured Data for LandSalesPro
 * Include this file in the <head> section of all public pages
 */

// Default SEO values
$seo_defaults = [
    'title' => 'LandSalesPro - Land Management & Plot Sales Software',
    'description' => 'LandSalesPro is a comprehensive land management software for land sellers, real estate developers and investors. Manage plots, track land sales, payments, expenses and map land polygons easily.',
    'keywords' => 'land management software, plot management software, real estate land sales software, land sales management system, plot tracking software, land mapping software, land developer software, real estate CRM, property plot management, land sales software africa',
    'url' => 'https://landsalespro.landsecure.co.tz',
    'image' => 'https://landsalespro.landsecure.co.tz/assets/images/og-image.png',
    'type' => 'website'
];

// Page-specific SEO (override defaults)
$page_seo = $page_seo ?? [];
$seo = array_merge($seo_defaults, $page_seo);

// Supported languages for hreflang
$supported_languages = [
    'en' => 'English',
    'sw' => 'Swahili',
    'fr' => 'French',
    'es' => 'Spanish',
    'ar' => 'Arabic',
    'de' => 'German',
    'pt' => 'Portuguese',
    'ru' => 'Russian',
    'zh' => 'Chinese',
    'hi' => 'Hindi',
    'ja' => 'Japanese',
    'ko' => 'Korean',
    'it' => 'Italian',
    'tr' => 'Turkish',
    'nl' => 'Dutch',
    'pl' => 'Polish',
    'th' => 'Thai',
    'id' => 'Indonesian',
    'vi' => 'Vietnamese',
    'fa' => 'Persian'
];

/**
 * Output SEO meta tags
 */
function outputSeoMeta($seo) {
    ?>
    <!-- Primary Meta Tags -->
    <title><?php echo htmlspecialchars($seo['title']); ?></title>
    <meta name="title" content="<?php echo htmlspecialchars($seo['title']); ?>">
    <meta name="description" content="<?php echo htmlspecialchars($seo['description']); ?>">
    <meta name="keywords" content="<?php echo htmlspecialchars($seo['keywords']); ?>">
    <meta name="robots" content="index, follow">
    <meta name="author" content="LandSecure">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="canonical" href="<?php echo htmlspecialchars($seo['url']); ?>">

    <!-- Open Graph / Facebook -->
    <meta property="og:type" content="<?php echo htmlspecialchars($seo['type']); ?>">
    <meta property="og:url" content="<?php echo htmlspecialchars($seo['url']); ?>">
    <meta property="og:title" content="<?php echo htmlspecialchars($seo['title']); ?>">
    <meta property="og:description" content="<?php echo htmlspecialchars($seo['description']); ?>">
    <meta property="og:image" content="<?php echo htmlspecialchars($seo['image']); ?>">
    <meta property="og:site_name" content="LandSalesPro">
    <meta property="og:locale" content="en_US">

    <!-- Twitter -->
    <meta property="twitter:card" content="summary_large_image">
    <meta property="twitter:url" content="<?php echo htmlspecialchars($seo['url']); ?>">
    <meta property="twitter:title" content="<?php echo htmlspecialchars($seo['title']); ?>">
    <meta property="twitter:description" content="<?php echo htmlspecialchars($seo['description']); ?>">
    <meta property="twitter:image" content="<?php echo htmlspecialchars($seo['image']); ?>">

    <!-- Additional SEO -->
    <meta name="geo.region" content="TZ">
    <meta name="geo.placename" content="Tanzania">
    <meta name="language" content="English">
    <meta name="revisit-after" content="7 days">
    <meta name="distribution" content="global">
    <meta name="rating" content="general">
    <?php
}

/**
 * Output hreflang tags for multilingual support
 */
function outputHreflangTags($base_url, $path = '') {
    global $supported_languages;
    foreach ($supported_languages as $code => $name) {
        echo '<link rel="alternate" hreflang="' . $code . '" href="' . $base_url . '/' . $code . '/' . $path . '" />' . "\n    ";
    }
    echo '<link rel="alternate" hreflang="x-default" href="' . $base_url . '/' . $path . '" />' . "\n";
}

/**
 * Output structured data for SoftwareApplication
 */
function outputSoftwareSchema($name = 'LandSalesPro', $description = null, $price = '50000', $currency = 'TZS') {
    $description = $description ?? 'Land management and plot sales software for land sellers and real estate developers.';
    ?>
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "SoftwareApplication",
        "name": "<?php echo $name; ?>",
        "operatingSystem": "Web",
        "applicationCategory": "BusinessApplication",
        "applicationSubCategory": "Real Estate Software",
        "description": "<?php echo $description; ?>",
        "url": "https://landsalespro.landsecure.co.tz",
        "downloadUrl": "https://landsalespro.landsecure.co.tz/register",
        "screenshot": "https://landsalespro.landsecure.co.tz/assets/images/screenshot.png",
        "softwareVersion": "1.0",
        "author": {
            "@type": "Organization",
            "name": "LandSecure",
            "url": "https://landsecure.co.tz"
        },
        "offers": {
            "@type": "Offer",
            "price": "<?php echo $price; ?>",
            "priceCurrency": "<?php echo $currency; ?>",
            "priceValidUntil": "<?php echo date('Y-12-31'); ?>"
        },
        "aggregateRating": {
            "@type": "AggregateRating",
            "ratingValue": "4.8",
            "ratingCount": "156"
        },
        "featureList": [
            "Plot Management",
            "Land Sales Tracking",
            "Payment Installments",
            "Agent Commission Tracking",
            "Land Mapping Integration",
            "Expense Management",
            "Sales Reports"
        ]
    }
    </script>
    <?php
}

/**
 * Output Organization schema
 */
function outputOrganizationSchema() {
    ?>
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "Organization",
        "name": "LandSecure",
        "alternateName": "LandSalesPro",
        "url": "https://landsecure.co.tz",
        "logo": "https://landsecure.co.tz/assets/images/logo.png",
        "sameAs": [
            "https://facebook.com/landsecure",
            "https://twitter.com/landsecure",
            "https://linkedin.com/company/landsecure"
        ],
        "contactPoint": {
            "@type": "ContactPoint",
            "telephone": "+255-XXX-XXX-XXX",
            "contactType": "customer service",
            "areaServed": "Africa",
            "availableLanguage": ["English", "Swahili"]
        }
    }
    </script>
    <?php
}

/**
 * Output FAQ schema
 */
function outputFaqSchema($faqs) {
    ?>
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "FAQPage",
        "mainEntity": [
            <?php
            $first = true;
            foreach ($faqs as $faq):
                if (!$first) echo ',';
                $first = false;
            ?>
            {
                "@type": "Question",
                "name": "<?php echo addslashes($faq['question']); ?>",
                "acceptedAnswer": {
                    "@type": "Answer",
                    "text": "<?php echo addslashes($faq['answer']); ?>"
                }
            }
            <?php endforeach; ?>
        ]
    }
    </script>
    <?php
}

/**
 * Output breadcrumb schema
 */
function outputBreadcrumbSchema($items) {
    ?>
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        "itemListElement": [
            <?php
            $position = 1;
            $first = true;
            foreach ($items as $name => $url):
                if (!$first) echo ',';
                $first = false;
            ?>
            {
                "@type": "ListItem",
                "position": <?php echo $position++; ?>,
                "name": "<?php echo addslashes($name); ?>",
                "item": "<?php echo $url; ?>"
            }
            <?php endforeach; ?>
        ]
    }
    </script>
    <?php
}
?>
