<?php

namespace App\Modules\Frontend\Controllers;

use App\Controllers\BaseController;
use App\Modules\Frontend\Models\TourModel;
use CodeIgniter\Exceptions\PageNotFoundException;

class ToursController extends BaseController 
{
    protected TourModel $model;

    public function initController(
        \CodeIgniter\HTTP\RequestInterface $request,
        \CodeIgniter\HTTP\ResponseInterface $response,
        \Psr\Log\LoggerInterface $logger
    ) {
        parent::initController($request, $response, $logger);
        $this->model = new TourModel();
    }

    public function index()
    {
        $tours = $this->model
            ->where('is_active', 1)
            ->orderBy('created_at', 'DESC')
            ->findAll();

        $this->data['tours'] = $tours;
        $this->data['meta_title'] = 'Mount Kenya Hiking Packages';
        $this->data['meta_desc'] =
            'Choose from our guided Mount Kenya trekking routes: Naro Moru, Sirimon, Chogoria – all-inclusive, safe, and unforgettable.';

        // Use full view path for clarity and reliability
        return view('App\Modules\Frontend\Views\tours\index', $this->data);
    }

    public function show(string $slug)
    {
        $tour = $this->model
            ->where('slug', $slug)
            ->where('is_active', 1)
            ->first();

        if (!$tour) {
            throw PageNotFoundException::forPageNotFound();
        }

        $this->data['tour'] = $tour;
        $this->data['meta_title'] = $tour['meta_title'] ?? $tour['title'];
        $this->data['meta_desc']  = $tour['meta_desc'] ?? '';

        return view('App\Modules\Frontend\Views\tours\show', $this->data);
    }
}