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

/**
 * Application_model — talent network applications.
 */
#[\AllowDynamicProperties]
class Application_model extends MY_Model {

	protected $table = 'applications';
	protected $order_by = 'created_at DESC';

	/** Applications filtered by status, with optional search. */
	public function filtered($status = NULL, $search = NULL, $limit = NULL, $offset = 0)
	{
		if ($status) $this->db->where('status', $status);
		if ($search)
		{
			$this->db->group_start()
				->like('name', $search)
				->or_like('email', $search)
				->or_like('category', $search)
				->group_end();
		}
		$this->db->order_by($this->order_by);
		if ($limit) $this->db->limit($limit, $offset);
		return $this->db->get($this->table)->result_array();
	}

	/** Count applications per status (for dashboard + filter badges). */
	public function status_counts()
	{
		$rows = $this->db->select('status, COUNT(*) AS total')
			->group_by('status')->get($this->table)->result_array();
		$out = array('pending' => 0, 'shortlisted' => 0, 'approved' => 0, 'rejected' => 0);
		foreach ($rows as $r) $out[$r['status']] = $r['total'];
		return $out;
	}

	/** Update the workflow status of an application. */
	public function set_status($id, $status, $notes = NULL)
	{
		$data = array('status' => $status);
		if ($notes !== NULL) $data['admin_notes'] = $notes;
		return $this->update($id, $data);
	}
}
