<?php
// application/controllers/Offers.php

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

class Offers extends CI_Controller
{

    // Loads the offers array once, reused by all methods
    private $offers;

    public function __construct()
    {
        parent::__construct();

        // Load the PHP array from our data file
        // APPPATH points to the application/ folder automatically
        $this->offers = include(APPPATH . 'data/offers_data.php');
    }

    // This method runs when someone visits /offers/dubai-tour
    // $slug will be "dubai-tour" (whatever comes after /offers/)
    public function detail($slug = NULL)
    {
        // Safety check — if no slug given, send them to 404
        if (empty($slug)) {
            show_404();
        }

        // Look up the slug in our array
        // e.g. $this->offers['dubai-tour']
        if (!isset($this->offers[$slug])) {
            // Slug not found — show 404 page
            show_404();
        }

        // Get the matching offer data
        $offer = $this->offers[$slug];

        // Package data to send to the view
        // $data is an array; each key becomes a variable in the view
        $data = [
            'page_title' => $offer['title'] . ' — Our Offers',
            'offer' => $offer,   // the full offer array
            'slug' => $slug,
        ];

        // Load the view — passes $data into it
        // CI3 looks for: application/views/offers/detail.php
        $this->load->view('offers/detail', $data);
    }

    // Optional: Offers listing page at /offers
    public function index()
    {
        $data = [
            'page_title' => 'All Offers',
            'offers' => $this->offers,
        ];

        $this->load->view('offers/index', $data);
    }
}