<?php

namespace App\Controllers;

class Seo extends BaseController
{
    public function health()
    {
        try { db_connect()->query('SELECT 1'); return $this->response->setJSON(['status'=>'ok','service'=>'tibaleta','time'=>date(DATE_ATOM)]); }
        catch(\Throwable $e){ log_message('error','Health check database failure: {message}',['message'=>$e->getMessage()]); return $this->response->setStatusCode(503)->setJSON(['status'=>'degraded','service'=>'tibaleta']); }
    }

    public function sitemap()
    {
        $paths=['','about','services','how-it-works','find-care','for-practitioners','partners','resources','faq','contact','privacy','terms'];
        $base=rtrim(base_url(),'/');
        $xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
        foreach($paths as $path){$xml.='<url><loc>'.htmlspecialchars($base.($path?'/'.$path:'/'),ENT_XML1).'</loc><changefreq>'.($path===''?'daily':'weekly').'</changefreq></url>';}
        $xml.='</urlset>';
        return $this->response->setContentType('application/xml')->setBody($xml);
    }
}
