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

/**
 * Admin / Applications — review talent network applications.
 */
#[\AllowDynamicProperties]
class Applications extends Admin_Controller {

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

	public function index()
	{
		$status = $this->input->get('status', TRUE);
		$search = $this->input->get('q', TRUE);

		$this->render('applications/index', array(
			'page_title'   => 'Talent Applications',
			'applications' => $this->application_model->filtered($status, $search),
			'counts'       => $this->application_model->status_counts(),
			'active'       => 'applications',
			'cur_status'   => $status,
			'cur_search'   => $search,
		));
	}

	public function view($id)
	{
		$app = $this->application_model->get($id);
		if ( ! $app) show_404();
		$this->render('applications/view', array(
			'page_title'  => 'Application — '.$app['name'],
			'app'         => $app,
			'active'      => 'applications',
		));
	}

	/** Update an application's status. */
	public function status($id)
	{
		$app = $this->application_model->get($id);
		if ( ! $app) show_404();

		$status = $this->input->post('status', TRUE);
		$notes  = $this->input->post('admin_notes', TRUE);
		$valid  = array('pending', 'shortlisted', 'approved', 'rejected');

		if (in_array($status, $valid, TRUE))
		{
			$this->application_model->set_status($id, $status, $notes);
			$this->log('Application '.$status, $app['name']);
			$this->session->set_flashdata('success', 'Application marked as '.$status.'.');
		}
		redirect('admin/applications/view/'.$id);
	}

	/**
	 * Promote an approved applicant into the talent roster as a draft profile.
	 */
	public function promote($id)
	{
		$app = $this->application_model->get($id);
		if ( ! $app) show_404();

		$slug = url_title($app['name'], '-', TRUE);

		$this->talent_model->insert(array(
			'name'     => $app['name'],
			'slug'     => $slug,
			'category' => $app['category'],
			'tagline'  => 'New talent',
			'bio'      => $app['bio'],
			'location' => $app['location'],
			'photo'    => $app['photo'],
			'stats'    => json_encode(array_filter(array(
				'followers' => $app['followers'],
			))),
			'socials'  => json_encode(array_filter(array(
				'instagram' => $app['instagram'],
			))),
			'availability' => 'available',
			'is_active'    => 0, // draft until reviewed
			'is_featured'  => 0,
		));

		$this->application_model->set_status($id, 'approved');
		$this->log('Promoted applicant to talent', $app['name']);
		$this->session->set_flashdata('success',
			'Applicant added to the talent roster as a draft profile. Review and publish it from Talent.');
		redirect('admin/talent');
	}

	public function delete($id)
	{
		$app = $this->application_model->get($id);
		if ($app)
		{
			$this->application_model->delete($id);
			$this->log('Deleted application', $app['name']);
			$this->session->set_flashdata('success', 'Application deleted.');
		}
		redirect('admin/applications');
	}
}
