<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for building types (Gebäudetypen)
 */

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/buildingtype.class.php');

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

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

$action = GETPOST('action', 'aZ09');
$levelType = GETPOST('level_type', 'alpha');

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

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

$buildingType = new BuildingType($db);

switch ($action) {
	case 'list':
		// Get all building types
		$types = $buildingType->fetchAll(1, $levelType);

		$result = array();
		foreach ($types as $t) {
			$result[] = array(
				'id' => $t->id,
				'ref' => $t->ref,
				'label' => $t->label,
				'label_short' => $t->label_short,
				'level_type' => $t->level_type,
				'level_type_label' => $t->getLevelTypeLabel(),
				'icon' => $t->icon,
				'color' => $t->color,
				'can_have_children' => $t->can_have_children,
				'is_system' => $t->is_system
			);
		}

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

	case 'list_grouped':
		// Get types grouped by level
		$grouped = $buildingType->fetchGroupedByLevel(1);

		$result = array();
		$levelTypes = BuildingType::getLevelTypes();

		foreach ($grouped as $level => $types) {
			$levelLabel = isset($levelTypes[$level]) ? $levelTypes[$level] : $level;
			$levelTypes_data = array();
			foreach ($types as $t) {
				$levelTypes_data[] = array(
					'id' => $t->id,
					'ref' => $t->ref,
					'label' => $t->label,
					'label_short' => $t->label_short,
					'icon' => $t->icon,
					'color' => $t->color,
					'can_have_children' => $t->can_have_children
				);
			}
			$result[] = array(
				'level_type' => $level,
				'level_type_label' => $levelLabel,
				'types' => $levelTypes_data
			);
		}

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

	case 'get':
		// Get single type details
		$typeId = GETPOSTINT('type_id');
		if ($typeId > 0 && $buildingType->fetch($typeId) > 0) {
			$response['success'] = true;
			$response['type'] = array(
				'id' => $buildingType->id,
				'ref' => $buildingType->ref,
				'label' => $buildingType->label,
				'label_short' => $buildingType->label_short,
				'description' => $buildingType->description,
				'level_type' => $buildingType->level_type,
				'level_type_label' => $buildingType->getLevelTypeLabel(),
				'icon' => $buildingType->icon,
				'color' => $buildingType->color,
				'can_have_children' => $buildingType->can_have_children,
				'is_system' => $buildingType->is_system
			);
		} else {
			$response['error'] = $langs->trans('ErrorRecordNotFound');
		}
		break;

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

echo json_encode($response);
