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

/**
 * Event_model — events / activations and their RSVPs.
 */
#[\AllowDynamicProperties]
class Event_model extends MY_Model {

	protected $table = 'events';
	protected $order_by = 'event_date DESC';

	/** Upcoming events (date today or later). */
	public function upcoming($limit = NULL)
	{
		$this->db->where('is_active', 1)
			->where('event_date >=', date('Y-m-d'))
			->order_by('event_date ASC');
		if ($limit) $this->db->limit($limit);
		return $this->db->get($this->table)->result_array();
	}

	/** Past events (date before today). */
	public function past($limit = NULL, $offset = 0)
	{
		$this->db->where('is_active', 1)
			->where('event_date <', date('Y-m-d'))
			->order_by('event_date DESC');
		if ($limit) $this->db->limit($limit, $offset);
		return $this->db->get($this->table)->result_array();
	}

	/** Featured events for the homepage. */
	public function featured($limit = 3)
	{
		return $this->get_all(array('is_active' => 1, 'is_featured' => 1), $limit);
	}

	/** Fetch a single active event by slug. */
	public function by_slug($slug)
	{
		return $this->get_by(array('slug' => $slug, 'is_active' => 1));
	}

	/** Record an RSVP for an event. */
	public function add_rsvp($data)
	{
		$data['created_at'] = date('Y-m-d H:i:s');
		$this->db->insert('event_rsvps', $data);
		return $this->db->insert_id();
	}

	/** Count RSVPs for an event. */
	public function rsvp_count($event_id)
	{
		return $this->db->where('event_id', $event_id)
			->count_all_results('event_rsvps');
	}

	/** List RSVPs for an event (admin). */
	public function rsvps($event_id)
	{
		return $this->db->where('event_id', $event_id)
			->order_by('created_at DESC')
			->get('event_rsvps')->result_array();
	}
}
