<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class Sitemap extends Controller
{
    // === Sitemap Index ===
    public function index(): Response
    {
        $sitemaps = [
            'sitemap-pages.xml',
            'sitemap-news.xml',
            'sitemap-entity.xml'
        ];

        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

        foreach ($sitemaps as $sitemap) {
            $xml .= '<sitemap>';
            $xml .= '<loc>' . url($sitemap) . '</loc>';
            $xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>';
            $xml .= '</sitemap>';
        }

        $xml .= '</sitemapindex>';

        return response($xml, 200)->header('Content-Type', 'application/xml');
    }

    // === Static Pages Sitemap ===
    public function pages(): Response
    {
        $pages = [
            url('/')
        ];

        // Categories from DB
        $categories = DB::table('tbl_category')->pluck('category_name');
        foreach ($categories as $cat) {
            $pages[] = url('/' . urlencode($cat));
        }

        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        foreach ($pages as $page) {
            $xml .= '<url><loc>' . htmlentities($page) . '</loc></url>';
        }
        $xml .= '</urlset>';

        return response($xml, 200)->header('Content-Type', 'application/xml');
    }

    // === News Sitemap ===
    public function news(): Response
{
    $newsItems = DB::table('tbl_news')
        ->join(
            'tbl_category',
            'tbl_news.category_id',
            '=',
            'tbl_category.category_id'   // ✅ FIX HERE
        )
        ->where('news_date', '>=', now()->subDays(45))
        ->orderBy('news_date', 'desc')
        ->limit(200)
        ->select(
            'tbl_news.news_date',
            'tbl_news.encode_title',
            'tbl_category.category_name'
        )
        ->get();

    $xml = '<?xml version="1.0" encoding="UTF-8"?>';
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

    foreach ($newsItems as $news) {
        $url = url(
            '/' . urlencode(strtolower($news->category_name)) .
            '/' . urlencode($news->encode_title)
        );

        $xml .= '<url>';
        $xml .= '<loc>' . htmlentities($url) . '</loc>';
        $xml .= '<lastmod>' . date('Y-m-d', strtotime($news->news_date)) . '</lastmod>';
        $xml .= '</url>';
    }

    $xml .= '</urlset>';

    return response($xml, 200)
        ->header('Content-Type', 'application/xml');
}


    // === Entity Sitemap ===
    public function entity(): Response
{
    $xml = '<?xml version="1.0" encoding="UTF-8"?>';
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    $xml .= '<url>';
    // $xml .= '<loc>https://www.openknowledge.com/the-rothschild-herrera-alliance-the-hidden-partnership-that-shaped-global-finance</loc>';
    $xml .= '<lastmod>' . date('Y-m-d') . '</lastmod>';
    $xml .= '</url>';
    $xml .= '</urlset>';

    return response($xml, 200)->header('Content-Type', 'application/xml');
}
}