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

/**
 * Admin / Portfolio — manage campaign / project case studies.
 */
#[\AllowDynamicProperties]
class Portfolio_admin extends Admin_Controller {

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

	public function index()
	{
		$this->render('portfolio/index', array(
			'page_title' => 'Portfolio',
			'projects'   => $this->portfolio_model->get_all(),
			'categories' => $this->config->item('portfolio_categories'),
		));
	}

	public function create()
	{
		$this->render('portfolio/form', array(
			'page_title' => 'Add Project',
			'project'    => $this->_blank(),
			'categories' => $this->config->item('portfolio_categories'),
			'talent'     => $this->talent_model->get_all(array('is_active' => 1)),
			'mode'       => 'create',
		));
	}

	public function edit($id)
	{
		$project = $this->portfolio_model->get($id);
		if ( ! $project) show_404();
		$this->render('portfolio/form', array(
			'page_title' => 'Edit Project',
			'project'    => $project,
			'categories' => $this->config->item('portfolio_categories'),
			'talent'     => $this->talent_model->get_all(array('is_active' => 1)),
			'mode'       => 'edit',
		));
	}

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

		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 'Title', '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/portfolio/edit/'.$id : 'admin/portfolio/create');
		}

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

		$data = array(
			'title'        => $title,
			'slug'         => $slug,
			'category'     => $this->input->post('category', TRUE),
			'brand'        => $this->input->post('brand', TRUE),
			'year'         => $this->input->post('year', TRUE),
			'excerpt'      => $this->input->post('excerpt', TRUE),
			'cs_brief'     => $this->input->post('cs_brief', TRUE),
			'cs_concept'   => $this->input->post('cs_concept', TRUE),
			'cs_execution' => $this->input->post('cs_execution', TRUE),
			'cs_results'   => $this->input->post('cs_results', TRUE),
			'video_type'   => $this->input->post('video_type', TRUE) ?: 'none',
			'youtube_id'   => youtube_id($this->input->post('youtube_id', TRUE)),
			'talent_ids'   => implode(',', (array) $this->input->post('talent_ids')),
			'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'),
			'meta_title'   => $this->input->post('meta_title', TRUE),
			'meta_desc'    => $this->input->post('meta_desc', TRUE),
		);

		// Cover image
		$cover = $this->upload('cover_image', 'portfolio');
		if ($cover) $data['cover_image'] = $cover;

		// Video file
		// Video file — uses the larger video size limit.
		$vid = $this->upload('video_file', 'portfolio', 'mp4|webm|mov',
			$this->config->item('upload_max_video') ?: 51200);
		if ($vid) $data['video_file'] = $vid;

		// Gallery (multi-upload, merged with existing)
		$new_gallery = $this->uploader->files('gallery', 'portfolio');
		$existing = $id ? decode_json($this->portfolio_model->get($id)['gallery']) : array();
		// keep ones not removed
		$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->portfolio_model->update($id, $data);
			$this->log('Updated project', $title);
			$msg = 'Project updated.';
		}
		else
		{
			$this->portfolio_model->insert($data);
			$this->log('Created project', $title);
			$msg = 'Project created.';
		}

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

	public function delete($id)
	{
		$project = $this->portfolio_model->get($id);
		if ($project)
		{
			$this->portfolio_model->delete($id);
			$this->log('Deleted project', $project['title']);
			$this->session->set_flashdata('success', 'Project deleted.');
		}
		redirect('admin/portfolio');
	}

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

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

	private function _blank()
	{
		return array(
			'id' => 0, 'title' => '', 'slug' => '', 'category' => '', 'brand' => '',
			'year' => date('Y'), 'cover_image' => '', 'excerpt' => '',
			'cs_brief' => '', 'cs_concept' => '', 'cs_execution' => '', 'cs_results' => '',
			'gallery' => '[]', 'video_type' => 'none', 'video_file' => '', 'youtube_id' => '',
			'talent_ids' => '', 'is_featured' => 0, 'is_active' => 1, 'sort_order' => 0,
			'meta_title' => '', 'meta_desc' => '',
		);
	}
}
