<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for medium types (Kabeltypen)
 */

if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');

$res = 0;
if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php";
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
if (!$res) die("Include of main fails");

dol_include_once('/kundenkarte/class/mediumtype.class.php');

header('Content-Type: application/json; charset=UTF-8');

$langs->loadLangs(array('kundenkarte@kundenkarte'));

$action = GETPOST('action', 'aZ09');
$systemId = GETPOSTINT('system_id');

$response = array('success' => false, 'error' => '');

// Security check
if (!$user->hasRight('kundenkarte', 'read')) {
	$response['error'] = $langs->trans('ErrorPermissionDenied');
	echo json_encode($response);
	exit;
}

$mediumType = new MediumType($db);

switch ($action) {
	case 'list':
		// Get all medium types for a system (or all)
		$types = $mediumType->fetchAllBySystem($systemId, 1);

		$result = array();
		foreach ($types as $t) {
			$specs = $t->getAvailableSpecsArray();
			$result[] = array(
				'id' => $t->id,
				'ref' => $t->ref,
				'label' => $t->label,
				'label_short' => $t->label_short,
				'category' => $t->category,
				'category_label' => $t->getCategoryLabel(),
				'fk_system' => $t->fk_system,
				'system_label' => $t->system_label,
				'default_spec' => $t->default_spec,
				'available_specs' => $specs,
				'color' => $t->color
			);
		}

		$response['success'] = true;
		$response['types'] = $result;
		break;

	case 'list_grouped':
		// Get types grouped by category
		$grouped = $mediumType->fetchGroupedByCategory($systemId);

		$result = array();
		foreach ($grouped as $category => $types) {
			$catTypes = array();
			foreach ($types as $t) {
				$catTypes[] = array(
					'id' => $t->id,
					'ref' => $t->ref,
					'label' => $t->label,
					'label_short' => $t->label_short,
					'default_spec' => $t->default_spec,
					'available_specs' => $t->getAvailableSpecsArray(),
					'color' => $t->color
				);
			}
			$result[] = array(
				'category' => $category,
				'category_label' => $types[0]->getCategoryLabel(),
				'types' => $catTypes
			);
		}

		$response['success'] = true;
		$response['groups'] = $result;
		break;

	case 'get':
		// Get single type details
		$typeId = GETPOSTINT('type_id');
		if ($typeId > 0 && $mediumType->fetch($typeId) > 0) {
			$response['success'] = true;
			$response['type'] = array(
				'id' => $mediumType->id,
				'ref' => $mediumType->ref,
				'label' => $mediumType->label,
				'label_short' => $mediumType->label_short,
				'category' => $mediumType->category,
				'category_label' => $mediumType->getCategoryLabel(),
				'default_spec' => $mediumType->default_spec,
				'available_specs' => $mediumType->getAvailableSpecsArray(),
				'color' => $mediumType->color,
				'description' => $mediumType->description
			);
		} else {
			$response['error'] = $langs->trans('ErrorRecordNotFound');
		}
		break;

	default:
		$response['error'] = 'Unknown action';
}

echo json_encode($response);
