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

/**
 * Events — upcoming & past events, event detail, RSVP.
 */
#[\AllowDynamicProperties]
class Events extends Public_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('event_model', 'ticket_model'));
	}

	/** Events landing — upcoming + past. */
	public function index()
	{
		$this->meta(array(
			'title'       => 'Events & Activations — Bloom Iris Agency',
			'description' => 'Fashion events, brand activations and pop-ups produced by Bloom Iris Agency.',
		));
		$this->render('events/index', array(
			'upcoming'   => $this->event_model->upcoming(),
			'past'       => $this->event_model->past(6),
			'event_types'=> $this->config->item('event_types'),
		));
	}

	/** Single event detail. */
	public function detail($slug)
	{
		$event = $this->event_model->by_slug($slug);
		if ( ! $event) show_404();

		$is_past = strtotime($event['event_date']) < strtotime(date('Y-m-d'));

		$this->meta(array(
			'title'       => ($event['meta_title'] ?: $event['title'].' — Bloom Iris Agency'),
			'description' => ($event['meta_desc'] ?: $event['excerpt']),
			'image'       => upload_url($event['cover_image']),
		));

		$this->render('events/detail', array(
			'event'      => $event,
			'gallery'    => decode_json($event['gallery']),
			'is_past'    => $is_past,
			'rsvp_count' => $this->event_model->rsvp_count($event['id']),
			'event_types'=> $this->config->item('event_types'),
		));
	}

	/** Handle a free-event RSVP submission. */
	public function rsvp($event_id)
	{
		$event = $this->event_model->get($event_id);
		if ( ! $event || ! $event['allow_rsvp'])
		{
			show_error('RSVP is not available for this event.', 400);
		}

		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Name', 'required|trim');
		$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
		$this->form_validation->set_rules('guests', 'Guests', 'integer|greater_than[0]|less_than[11]');

		if ($this->form_validation->run() === FALSE)
		{
			$this->session->set_flashdata('error', 'Please complete all required fields correctly.');
			redirect('events/view/'.$event['slug']);
		}

		$this->event_model->add_rsvp(array(
			'event_id' => $event['id'],
			'name'     => $this->input->post('name', TRUE),
			'email'    => $this->input->post('email', TRUE),
			'phone'    => $this->input->post('phone', TRUE),
			'guests'   => (int) $this->input->post('guests') ?: 1,
		));

		// Notify the agency.
		$this->load->library('mailer');
		$this->mailer->notify_admin(
			'New RSVP — '.$event['title'],
			'New Event RSVP',
			'<p><strong>'.html_escape($this->input->post('name', TRUE)).'</strong> has RSVP\'d for '
			.'<strong>'.html_escape($event['title']).'</strong>.</p>'
			.'<p>Email: '.html_escape($this->input->post('email', TRUE)).'</p>'
		);

		$this->session->set_flashdata('success', 'Your RSVP is confirmed — see you there!');
		redirect('events/view/'.$event['slug']);
	}
}
