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

/**
 * Talent_model — talent roster.
 */
#[\AllowDynamicProperties]
class Talent_model extends MY_Model {

	protected $table = 'talent';
	protected $order_by = 'sort_order ASC, name ASC';

	/** Active talent, optionally filtered by category. */
	public function active($category = NULL, $limit = NULL, $offset = 0)
	{
		$where = array('is_active' => 1);
		if ($category) $where['category'] = $category;
		return $this->get_all($where, $limit, $offset);
	}

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

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

	/** Related talent in the same category. */
	public function related($category, $exclude_id, $limit = 4)
	{
		return $this->db->where('is_active', 1)
			->where('category', $category)
			->where('id !=', $exclude_id)
			->order_by('id', 'RANDOM')
			->limit($limit)
			->get($this->table)->result_array();
	}

	/** Resolve a list of talent IDs to rows (for portfolio credits). */
	public function by_ids($ids)
	{
		$ids = array_filter(array_map('intval', (array) $ids));
		if (empty($ids)) return array();
		return $this->db->where_in('id', $ids)
			->get($this->table)->result_array();
	}

	/** Category counts among active talent. */
	public function category_counts()
	{
		$rows = $this->db->select('category, COUNT(*) AS total')
			->where('is_active', 1)
			->group_by('category')
			->get($this->table)->result_array();
		$out = array();
		foreach ($rows as $r) $out[$r['category']] = $r['total'];
		return $out;
	}
}
