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

/**
 * Team_model — founders & team profiles.
 */
#[\AllowDynamicProperties]
class Team_model extends MY_Model {

	protected $table = 'team';
	protected $order_by = 'sort_order ASC, id ASC';
	
	// Fix: Match the parent method signature exactly
	public function get_all($where = [], $limit = null, $offset = 0)
	{
		$this->db->select('*'); // Explicitly select all columns
		$this->db->from($this->table);
		
		if (!empty($where)) {
			$this->db->where($where);
		}
		
		if ($limit !== null) {
			$this->db->limit($limit, $offset);
		}
		
		if ($this->order_by) {
			$this->db->order_by($this->order_by);
		}
		
		$query = $this->db->get();
		$results = $query->result_array();
		
		// Ensure socials is always a valid JSON string
		foreach ($results as &$row) {
			if (!isset($row['socials']) || $row['socials'] === NULL || $row['socials'] === '') {
				$row['socials'] = '{}';
			}
		}
		
		return $results;
	}
	
	// Override get to ensure we get the socials field
	public function get($id)
	{
		$this->db->select('*');
		$this->db->from($this->table);
		$this->db->where('id', $id);
		$query = $this->db->get();
		$result = $query->row_array();
		
		// Ensure socials is always a valid JSON string
		if ($result && (!isset($result['socials']) || $result['socials'] === NULL || $result['socials'] === '')) {
			$result['socials'] = '{}';
		}
		
		return $result;
	}

	/** All active team members. */
	public function active()
	{
		return $this->get_all(['is_active' => 1]);
	}

	/** Founders only (editorial layout on the About page). */
	public function founders()
	{
		return $this->get_all(['is_active' => 1, 'is_founder' => 1]);
	}

	/** Non-founder team members. */
	public function staff()
	{
		return $this->get_all(['is_active' => 1, 'is_founder' => 0]);
	}
}