<?php

// =====================================================
// IMFASHINI ATTIRE
// DYNAMIC XML SITEMAP
// Automatically includes all products
// =====================================================

require_once __DIR__ . '/database.php';

// Tell browsers and search engines this is XML
header('Content-Type: application/xml; charset=UTF-8');

// Base website URL
$base_url = 'https://imfashini.co.za';

// Escape XML safely
function xml_escape($value)
{
    return htmlspecialchars(
        (string) $value,
        ENT_XML1 | ENT_QUOTES,
        'UTF-8'
    );
}

// =====================================================
// STATIC PUBLIC PAGES
// =====================================================

$static_pages = [
    [
        'url' => '/',
        'changefreq' => 'daily',
        'priority' => '1.0'
    ],
    [
        'url' => '/about-us.php',
        'changefreq' => 'monthly',
        'priority' => '0.8'
    ],
    [
        'url' => '/contact.php',
        'changefreq' => 'monthly',
        'priority' => '0.8'
    ],
    [
        'url' => '/men.php',
        'changefreq' => 'weekly',
        'priority' => '0.9'
    ],
    [
        'url' => '/women.php',
        'changefreq' => 'weekly',
        'priority' => '0.9'
    ],
    [
        'url' => '/accessories.php',
        'changefreq' => 'weekly',
        'priority' => '0.9'
    ],
    [
        'url' => '/best-selling.php',
        'changefreq' => 'weekly',
        'priority' => '0.8'
    ],
    [
        'url' => '/new-arrival.php',
        'changefreq' => 'weekly',
        'priority' => '0.8'
    ],
    [
        'url' => '/affiliate.php',
        'changefreq' => 'monthly',
        'priority' => '0.6'
    ],
    [
        'url' => '/privacy-policy.php',
        'changefreq' => 'yearly',
        'priority' => '0.5'
    ],
    [
        'url' => '/terms-and-conditions.php',
        'changefreq' => 'yearly',
        'priority' => '0.5'
    ],
    [
        'url' => '/cookie-policy.php',
        'changefreq' => 'yearly',
        'priority' => '0.5'
    ],
    [
        'url' => '/disclaimer.php',
        'changefreq' => 'yearly',
        'priority' => '0.5'
    ]
];

// =====================================================
// START XML
// =====================================================

echo '<?xml version="1.0" encoding="UTF-8"?>';

echo "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
echo "\n";

// =====================================================
// STATIC PAGES
// =====================================================

foreach ($static_pages as $page) {


echo "    <url>\n";

echo '        <loc>'
    . xml_escape($base_url . $page['url'])
    . "</loc>\n";

echo '        <lastmod>'
    . date('Y-m-d')
    . "</lastmod>\n";

echo '        <changefreq>'
    . xml_escape($page['changefreq'])
    . "</changefreq>\n";

echo '        <priority>'
    . xml_escape($page['priority'])
    . "</priority>\n";

echo "    </url>\n";


}

// =====================================================
// PRODUCTS
// =====================================================

$sql = "
SELECT
id,
updated_at,
created_at
FROM products
WHERE id > 0
ORDER BY id ASC
";

$result = mysqli_query($conn, $sql);

if ($result) {


while ($product = mysqli_fetch_assoc($result)) {

    $product_id = (int) $product['id'];

    // Prefer updated_at.
    // Fall back to created_at if necessary.
    $date_source =
        !empty($product['updated_at'])
            ? $product['updated_at']
            : $product['created_at'];

    $lastmod = date(
        'Y-m-d',
        strtotime($date_source)
    );

    $product_url =
        $base_url .
        '/product-details.php?id=' .
        $product_id;

    echo "    <url>\n";

    echo '        <loc>'
        . xml_escape($product_url)
        . "</loc>\n";

    echo '        <lastmod>'
        . xml_escape($lastmod)
        . "</lastmod>\n";

    echo "        <changefreq>weekly</changefreq>\n";

    echo "        <priority>0.8</priority>\n";

    echo "    </url>\n";
}

mysqli_free_result($result);


}

// =====================================================
// END XML
// =====================================================

echo "</urlset>";
?>
