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

/**
 * Admin / Talent — manage the talent roster.
 */
#[\AllowDynamicProperties]
class Talent_admin extends Admin_Controller {

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

	public function index()
	{
		$this->render('talent/index', array(
			'page_title' => 'Talent Roster',
			'talent'     => $this->talent_model->get_all(),
			'categories' => $this->config->item('talent_categories'),
		));
	}

	public function create()
	{
		$this->render('talent/form', array(
			'page_title' => 'Add Talent',
			'talent'     => $this->_blank(),
			'categories' => $this->config->item('talent_categories'),
			'mode'       => 'create',
		));
	}

	public function edit($id)
	{
		$talent = $this->talent_model->get($id);
		if ( ! $talent) show_404();
		$this->render('talent/form', array(
			'page_title' => 'Edit Talent',
			'talent'     => $talent,
			'categories' => $this->config->item('talent_categories'),
			'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('category', 'Category', 'required');

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

		$name = $this->input->post('name', TRUE);
		$slug = $this->input->post('slug', TRUE) ?: url_title($name, '-', TRUE);

		// Stats (instagram/height/etc.) + socials as JSON
		$stats = array(
			'followers' => $this->input->post('stat_followers', TRUE),
			'height'    => $this->input->post('stat_height', TRUE),
			'based'     => $this->input->post('stat_based', TRUE),
		);
		$socials = array(
			'instagram' => $this->input->post('social_instagram', TRUE),
			'tiktok'    => $this->input->post('social_tiktok', TRUE),
			'youtube'   => $this->input->post('social_youtube', TRUE),
		);

		$data = array(
			'name'        => $name,
			'slug'        => $slug,
			'category'    => $this->input->post('category', TRUE),
			'tagline'     => $this->input->post('tagline', TRUE),
			'bio'         => $this->input->post('bio', TRUE),
			'location'    => $this->input->post('location', TRUE),
			'availability'=> $this->input->post('availability', TRUE) ?: 'available',
			'stats'       => json_encode(array_filter($stats)),
			'socials'     => json_encode(array_filter($socials)),
			'is_featured' => $this->input->post('is_featured') ? 1 : 0,
			'is_active'   => $this->input->post('is_active') ? 1 : 0,
			'sort_order'  => (int) $this->input->post('sort_order'),
		);

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

		// Gallery
		$new_gallery = $this->uploader->files('gallery', 'talent');
		$existing = $id ? decode_json($this->talent_model->get($id)['gallery']) : array();
		$keep = (array) $this->input->post('keep_gallery');
		$existing = array_values(array_intersect($existing, $keep));
		$data['gallery'] = json_encode(array_merge($existing, $new_gallery));

		if ($id)
		{
			$this->talent_model->update($id, $data);
			$this->log('Updated talent', $name);
			$msg = 'Talent profile updated.';
		}
		else
		{
			$this->talent_model->insert($data);
			$this->log('Created talent', $name);
			$msg = 'Talent profile created.';
		}

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

	public function delete($id)
	{
		$t = $this->talent_model->get($id);
		if ($t)
		{
			$this->talent_model->delete($id);
			$this->log('Deleted talent', $t['name']);
			$this->session->set_flashdata('success', 'Talent profile deleted.');
		}
		redirect('admin/talent');
	}

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

	public function feature($id)
	{
		$t = $this->talent_model->get($id);
		if ($t) $this->talent_model->update($id, array('is_featured' => $t['is_featured'] ? 0 : 1));
		redirect('admin/talent');
	}

	private function _blank()
	{
		return array(
			'id' => 0, 'name' => '', 'slug' => '', 'category' => '', 'tagline' => '',
			'bio' => '', 'photo' => '', 'cover_photo' => '', 'gallery' => '[]',
			'stats' => '{}', 'socials' => '{}', 'location' => '',
			'availability' => 'available', 'is_featured' => 0, 'is_active' => 1, 'sort_order' => 0,
		);
	}
}
