<?php

namespace App\Controllers;

use App\Models\SettingModel;
use App\Models\NewsModel;
use App\Models\EventModel;
use App\Models\GalleryModel;

class Sitemap extends BaseController
{
    /**
     * Generate the XML sitemap.
     *
     * @return string
     */
    public function index()
    {
        $settingModel = new SettingModel();
        $siteName = $settingModel->get('site_name') ?? 'SekolahKu';

        $newsModel = new NewsModel();
        $news = $newsModel->where('status', 'published')
            ->select('slug, updated_at, published_at')
            ->findAll();

        $eventModel = new EventModel();
        $events = $eventModel->where('show', 1)
            ->select('slug, updated_at, event_date')
            ->findAll();

        $galleryModel = new GalleryModel();
        $galleries = $galleryModel->select('updated_at')
            ->where('show', 1)
            ->orderBy('sort_order', 'ASC')
            ->findAll();

        $this->response->setContentType('application/xml');
        return view('sitemap', [
            'site_name'  => $siteName,
            'base_url'   => base_url(),
            'news'       => $news,
            'events'     => $events,
            'galleries'  => $galleries,
        ]);
    }
}
