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

/**
 * Portfolio — campaign showcase with category filtering and case studies.
 */
#[\AllowDynamicProperties]
class Portfolio extends Public_Controller {

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

	/** All projects (client-side filterable). */
	public function index()
	{
		$this->meta(array(
			'title'       => 'Portfolio — Bloom Iris Agency',
			'description' => 'Campaigns, editorials and brand collaborations by Bloom Iris Agency.',
		));
		$this->render('portfolio/index', array(
			'projects'   => $this->portfolio_model->active(),
			'categories' => $this->config->item('portfolio_categories'),
			'counts'     => $this->portfolio_model->category_counts(),
			'active_cat' => 'all',
		));
	}

	/** Projects filtered by category (server-side, for SEO-friendly URLs). */
	public function category($slug)
	{
		$categories = $this->config->item('portfolio_categories');
		if ( ! isset($categories[$slug])) show_404();

		$this->meta(array(
			'title' => $categories[$slug].' — Portfolio — Bloom Iris Agency',
		));
		$this->render('portfolio/index', array(
			'projects'   => $this->portfolio_model->active($slug),
			'categories' => $categories,
			'counts'     => $this->portfolio_model->category_counts(),
			'active_cat' => $slug,
		));
	}

	/** Single project case study. */
	public function detail($slug)
	{
		$project = $this->portfolio_model->by_slug($slug);
		if ( ! $project) show_404();

		$this->portfolio_model->add_view($project['id']);
		$categories = $this->config->item('portfolio_categories');

		$this->meta(array(
			'title'       => ($project['meta_title'] ?: $project['title'].' — Bloom Iris Agency'),
			'description' => ($project['meta_desc'] ?: $project['excerpt']),
			'image'       => upload_url($project['cover_image']),
		));

		$this->render('portfolio/detail', array(
			'project'    => $project,
			'gallery'    => decode_json($project['gallery']),
			'credits'    => $this->talent_model->by_ids(explode(',', (string) $project['talent_ids'])),
			'related'    => $this->portfolio_model->related($project['category'], $project['id'], 3),
			'categories' => $categories,
		));
	}
}
