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

/* ==========================================================================
 * BLOOM IRIS AGENCY — UI HELPER
 * Reusable UI partials: status pills, badges, flash messages, icons.
 * ======================================================================= */

if ( ! function_exists('status_pill'))
{
	/** Render a coloured status pill for a pipeline/workflow status. */
	function status_pill($status)
	{
		$map = array(
			'new'         => array('New', 'pill-blue'),
			'reviewed'    => array('Reviewed', 'pill-amber'),
			'converted'   => array('Converted', 'pill-green'),
			'closed'      => array('Closed', 'pill-grey'),
			'pending'     => array('Pending', 'pill-amber'),
			'shortlisted' => array('Shortlisted', 'pill-blue'),
			'approved'    => array('Approved', 'pill-green'),
			'rejected'    => array('Rejected', 'pill-red'),
			'paid'        => array('Paid', 'pill-green'),
			'failed'      => array('Failed', 'pill-red'),
			'draft'       => array('Draft', 'pill-grey'),
			'published'   => array('Published', 'pill-green'),
			'scheduled'   => array('Scheduled', 'pill-amber'),
			'available'   => array('Available', 'pill-green'),
			'booked'      => array('Booked', 'pill-amber'),
			'unavailable' => array('Unavailable', 'pill-grey'),
		);
		$key = strtolower((string) $status);
		$row = isset($map[$key]) ? $map[$key] : array(ucfirst($key), 'pill-grey');
		return '<span class="pill '.$row[1].'">'.html_escape($row[0]).'</span>';
	}
}

if ( ! function_exists('flash'))
{
	/** Render flash messages set via the session (success / error). */
	function flash()
	{
		$CI =& get_instance();
		$out = '';
		if ($msg = $CI->session->flashdata('success'))
		{
			$out .= '<div class="alert alert-success">'.html_escape($msg).'</div>';
		}
		if ($msg = $CI->session->flashdata('error'))
		{
			$out .= '<div class="alert alert-error">'.html_escape($msg).'</div>';
		}
		if ($msg = $CI->session->flashdata('info'))
		{
			$out .= '<div class="alert alert-info">'.html_escape($msg).'</div>';
		}
		return $out;
	}
}

if ( ! function_exists('bloom_icon'))
{
	/**
	 * Return an inline SVG icon by key. Keeps the platform dependency-free
	 * (no icon font). Stroke inherits currentColor.
	 */
	function bloom_icon($name, $size = 24)
	{
		$paths = array(
			'star'     => '<path d="M12 2l3 6.9 7.6.6-5.8 5 1.8 7.4L12 24l-6.4-2.5 1.8-7.4-5.8-5 7.6-.6z"/>',
			'handshake'=> '<path d="M11 17l-3 3-4-4 6-6 3 3"/><path d="M13 7l3-3 4 4-6 6-3-3"/>',
			'palette'  => '<circle cx="12" cy="12" r="9"/><circle cx="8" cy="9" r="1"/><circle cx="16" cy="9" r="1"/><circle cx="9" cy="15" r="1"/>',
			'calendar' => '<rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 9h18M8 3v4M16 3v4"/>',
			'camera'   => '<path d="M3 8h4l2-3h6l2 3h4v12H3z"/><circle cx="12" cy="13" r="3.5"/>',
			'film'     => '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M7 4v16M17 4v16M3 9h4M3 15h4M17 9h4M17 15h4"/>',
			'arrow'    => '<path d="M5 12h14M13 6l6 6-6 6"/>',
			'menu'     => '<path d="M4 7h16M4 12h16M4 17h16"/>',
			'close'    => '<path d="M6 6l12 12M18 6L6 18"/>',
			'play'     => '<path d="M8 5v14l11-7z"/>',
			'instagram'=> '<rect x="3" y="3" width="18" height="18" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="1"/>',
			'mail'     => '<rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/>',
			'phone'    => '<path d="M5 4h4l2 5-3 2a12 12 0 006 6l2-3 5 2v4a2 2 0 01-2 2A16 16 0 013 6a2 2 0 012-2z"/>',
			'pin'      => '<path d="M12 21s-7-5.6-7-11a7 7 0 0114 0c0 5.4-7 11-7 11z"/><circle cx="12" cy="10" r="2.5"/>',
			'check'    => '<path d="M5 13l4 4L19 7"/>',
			'grid'     => '<rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/>',
			'users'    => '<circle cx="9" cy="8" r="3.5"/><path d="M3 20c0-3.5 3-5.5 6-5.5s6 2 6 5.5"/><path d="M16 5a3 3 0 010 6M21 20c0-3-2-4.8-4.5-5.2"/>',
			'inbox'    => '<path d="M3 13l3-9h12l3 9v6H3z"/><path d="M3 13h5l2 3h4l2-3h5"/>',
			'settings' => '<circle cx="12" cy="12" r="3"/><path d="M19 12a7 7 0 00-.2-1.6l2-1.5-2-3.5-2.4 1a7 7 0 00-2.8-1.6L13 1h-4l-.6 2.3A7 7 0 005.6 4.9l-2.4-1-2 3.5 2 1.5A7 7 0 003 12c0 .5 0 1 .2 1.6l-2 1.5 2 3.5 2.4-1a7 7 0 002.8 1.6L9 23h4l.6-2.3a7 7 0 002.8-1.6l2.4 1 2-3.5-2-1.5c.1-.5.2-1 .2-1.6z"/>',
			'logout'   => '<path d="M9 5H5v14h4M14 12H7M14 12l-3-3M14 12l-3 3M16 5h3v14h-3"/>',
			'edit'     => '<path d="M4 20h4L20 8l-4-4L4 16z"/>',
			'trash'    => '<path d="M5 7h14M9 7V4h6v3M6 7l1 13h10l1-13"/>',
			'plus'     => '<path d="M12 5v14M5 12h14"/>',
			'search'   => '<circle cx="11" cy="11" r="7"/><path d="M21 21l-5-5"/>',
			'chart'    => '<path d="M4 20V10M10 20V4M16 20v-7M22 20H2"/>',
			'image'    => '<rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="9" cy="10" r="2"/><path d="M21 17l-5-5-8 8"/>',
			'whatsapp' => '<path d="M12 3a9 9 0 00-7.7 13.6L3 21l4.5-1.2A9 9 0 1012 3z"/><path d="M8.5 8.5c0 4 3 7 7 7"/>',
			'quote'    => '<path d="M7 7H4v5h3v-2c0 2-1 3-3 3v2c3 0 5-2 5-5V7zM17 7h-3v5h3v-2c0 2-1 3-3 3v2c3 0 5-2 5-5V7z"/>',
			'sparkle'  => '<path d="M12 3l2 5 5 2-5 2-2 5-2-5-5-2 5-2z"/>',
			'tag'      => '<path d="M3 11l8-8 10 10-8 8z"/><circle cx="8" cy="8" r="1.5"/>',
			'ticket'   => '<path d="M4 7a2 2 0 012-2h12a2 2 0 012 2 2 2 0 000 4 2 2 0 000 4 2 2 0 01-2 2H6a2 2 0 01-2-2 2 2 0 000-4 2 2 0 000-4z"/>',
			'upload'   => '<path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M12 16V3M7 8l5-5 5 5"/>',
		);
		$inner = isset($paths[$name]) ? $paths[$name] : $paths['sparkle'];
		return '<svg class="icon icon-'.$name.'" width="'.$size.'" height="'.$size.'" '
			.'viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" '
			.'stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'.$inner.'</svg>';
	}
}

if ( ! function_exists('val_errors'))
{
	/** Render CI form validation errors as a styled alert (if any). */
	function val_errors()
	{
		$errors = validation_errors('<li>', '</li>');
		if ($errors === '') return '';
		return '<div class="alert alert-error"><ul class="error-list">'.$errors.'</ul></div>';
	}
}

if ( ! function_exists('empty_state'))
{
	/** Render a friendly empty-state block. */
	function empty_state($message = 'Nothing here yet.', $icon = 'sparkle')
	{
		return '<div class="empty-state">'.bloom_icon($icon, 40)
			.'<p>'.html_escape($message).'</p></div>';
	}
}
