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

/**
 * Admin / Team — manage agency team & founder profiles.
 */
#[\AllowDynamicProperties]
class Team_admin extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('team_model');
	}

	public function index()
	{
		$this->render('team/index', array(
			'page_title' => 'Team',
			'members'    => $this->team_model->get_all(),
		));
	}

	public function create()
	{
		$this->render('team/form', array(
			'page_title' => 'Add Team Member',
			'member'     => $this->_blank(),
			'mode'       => 'create',
		));
	}

	public function edit($id)
	{
		$member = $this->team_model->get($id);
		if ( ! $member) show_404();
		
		// Ensure socials key exists
		if (!isset($member['socials'])) {
			$member['socials'] = '{}';
		}
		
		$this->render('team/form', array(
			'page_title' => 'Edit Team Member',
			'member'     => $member,
			'mode'       => 'edit',
		));
	}

	public function save()
	{
		$id = (int) $this->input->post('id');

		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Name', 'required|trim');
		$this->form_validation->set_rules('role', 'Role', 'required|trim');

		if ($this->form_validation->run() === FALSE)
		{
			$this->session->set_flashdata('error', strip_tags(validation_errors()));
			redirect($id ? 'admin/team/edit/'.$id : 'admin/team/create');
		}

		$socials = array(
			'instagram' => $this->input->post('social_instagram', TRUE),
			'linkedin'  => $this->input->post('social_linkedin', TRUE),
		);

		// Remove empty values
		$socials = array_filter($socials);
		
		$data = array(
			'name'       => $this->input->post('name', TRUE),
			'role'       => $this->input->post('role', TRUE),
			'bio'        => $this->input->post('bio', TRUE),
			'socials'    => !empty($socials) ? json_encode($socials) : '{}',
			'is_founder' => (int) $this->input->post('is_founder') ? 1 : 0,
			'is_active'  => (int) $this->input->post('is_active') ? 1 : 0,
			'sort_order' => (int) $this->input->post('sort_order'),
		);

		// Handle photo upload
		$photo = $this->upload('photo', 'team');
		if ($photo) $data['photo'] = $photo;

		if ($id)
		{
			$this->team_model->update($id, $data);
			$this->log('Updated team member', $data['name']);
			$msg = 'Team member updated.';
		}
		else
		{
			$this->team_model->insert($data);
			$this->log('Created team member', $data['name']);
			$msg = 'Team member added.';
		}

		$this->session->set_flashdata('success', $msg);
		redirect('admin/team');
	}

	public function delete($id)
	{
		$m = $this->team_model->get($id);
		if ($m)
		{
			$this->team_model->delete($id);
			$this->log('Deleted team member', $m['name']);
			$this->session->set_flashdata('success', 'Team member deleted.');
		}
		redirect('admin/team');
	}

	public function toggle($id)
	{
		$m = $this->team_model->get($id);
		if ($m) $this->team_model->update($id, array('is_active' => $m['is_active'] ? 0 : 1));
		redirect('admin/team');
	}

	private function _blank()
	{
		return array(
			'id' => 0, 
			'name' => '', 
			'role' => '', 
			'photo' => '', 
			'bio' => '',
			'socials' => '{}',  // Ensure this is set
			'is_founder' => 0, 
			'is_active' => 1, 
			'sort_order' => 0,
		);
	}
}