<?php

use Illuminate\Support\Facades\DB;

require __DIR__.'/../afro-media/vendor/autoload.php';
$app = require_once __DIR__.'/../afro-media/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

header('Content-Type: application/xml; charset=utf-8');

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// Static pages
$urls = [
    url('/'),
    url('/songs'),
    url('/videos'),
    url('/lyrics'),
    url('/news'),
];

foreach ($urls as $loc) {
    echo "<url><loc>$loc</loc><changefreq>daily</changefreq><priority>0.8</priority></url>";
}

// Posts
$posts = DB::table('posts')->where('status', 'published')->get();

foreach ($posts as $post) {
    $link = url('/' . $post->slug);
    $lastmod = date('c', strtotime($post->updated_at));

    echo "<url>
        <loc>$link</loc>
        <lastmod>$lastmod</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>";
}

echo '</urlset>';