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

/**
 * Media_model — centralised media library.
 */
#[\AllowDynamicProperties]
class Media_model extends MY_Model {

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

	/** Media filtered by folder and/or type. */
	public function filtered($folder = NULL, $type = NULL, $limit = NULL, $offset = 0)
	{
		if ($folder) $this->db->where('folder', $folder);
		if ($type)   $this->db->where('file_type', $type);
		$this->db->order_by($this->order_by);
		if ($limit) $this->db->limit($limit, $offset);
		return $this->db->get($this->table)->result_array();
	}

	/** Distinct folders with item counts. */
	public function folders()
	{
		$rows = $this->db->select('folder, COUNT(*) AS total')
			->group_by('folder')->get($this->table)->result_array();
		$out = array();
		foreach ($rows as $r) $out[$r['folder']] = $r['total'];
		return $out;
	}
}
