<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Dynamic XML sitemap for Aboriginal Hire.
 *
 * Outputs sitemap.xml live from the database so it always reflects the
 * current set of active jobs, categories and blog posts — no manual upkeep.
 *
 * Route (add to application/config/routes.php):
 *   $route['sitemap\.xml'] = 'Sitemap/index';
 *
 * Then in Google Search Console submit:  https://aboriginalhire.ca/sitemap.xml
 */
class Sitemap extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->database();
	}

	public function index()
	{
		$base = rtrim(base_url(), '/') . '/';
		$urls = array();

		// ---- Static / high-value pages ---------------------------------
		$urls[] = array('loc' => $base,                      'priority' => '1.0', 'changefreq' => 'daily');
		$urls[] = array('loc' => $base . 'Jobs/jobslisting', 'priority' => '0.9', 'changefreq' => 'daily');
		$urls[] = array('loc' => $base . 'Aboutus',          'priority' => '0.5', 'changefreq' => 'monthly');
		$urls[] = array('loc' => $base . 'Pricing',          'priority' => '0.5', 'changefreq' => 'monthly');
		$urls[] = array('loc' => $base . 'Blog/allblogs',    'priority' => '0.6', 'changefreq' => 'weekly');

		// ---- Active job postings ---------------------------------------
		// Uses ONLY confirmed names — this mirrors the exact query that ran
		// successfully in phpMyAdmin:
		//   SELECT id_posted_job FROM jb_post_a_job WHERE p_j_active_status = 'Active'
		// The result is guarded so a failed query can never crash the script.
		$this->db->select('id_posted_job');
		$this->db->from('jb_post_a_job');
		$this->db->where('p_j_active_status', 'Active');
		$query = $this->db->get();

		if ($query !== FALSE) {
			foreach ($query->result() as $job) {
				$urls[] = array(
					'loc'        => $base . 'Jobs/jobsdetails/' . $job->id_posted_job,
					'priority'   => '0.8',
					'changefreq' => 'weekly'
				);
			}
		}

		// ---- Build XML --------------------------------------------------
		$xml  = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
		foreach ($urls as $u) {
			$xml .= '  <url>' . "\n";
			$xml .= '    <loc>' . htmlspecialchars($u['loc'], ENT_XML1, 'UTF-8') . '</loc>' . "\n";
			if (!empty($u['lastmod'])) {
				$xml .= '    <lastmod>' . $u['lastmod'] . '</lastmod>' . "\n";
			}
			if (!empty($u['changefreq'])) {
				$xml .= '    <changefreq>' . $u['changefreq'] . '</changefreq>' . "\n";
			}
			if (!empty($u['priority'])) {
				$xml .= '    <priority>' . $u['priority'] . '</priority>' . "\n";
			}
			$xml .= '  </url>' . "\n";
		}
		$xml .= '</urlset>';

		// ---- Output XML -------------------------------------------------
		// Wipe EVERY output buffer (CodeIgniter's included) so stray whitespace
		// from any loaded config file cannot precede the XML, then send it
		// directly and stop — this bypasses CI's output handling entirely.
		while (ob_get_level()) {
			@ob_end_clean();
		}
		header('Content-Type: application/xml; charset=utf-8');
		echo $xml;
		exit;
	}
}