<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for field autocomplete suggestions
 * Returns unique values from saved anlage field values
 */

// Load Dolibarr environment
$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 && file_exists("../../../main.inc.php")) {
	$res = @include "../../../main.inc.php";
}
if (!$res) {
	die("Include of main fails");
}

header('Content-Type: application/json');

// Check permissions
if (!$user->hasRight('kundenkarte', 'read')) {
	echo json_encode(array('error' => 'Access denied'));
	exit;
}

$action = GETPOST('action', 'aZ09');
$fieldCode = GETPOST('field_code', 'aZ09');
$query = GETPOST('query', 'alphanohtml');
$typeId = GETPOSTINT('type_id');

if ($action == 'suggest') {
	$suggestions = array();

	if (empty($fieldCode)) {
		echo json_encode(array('suggestions' => $suggestions));
		exit;
	}

	// Get unique saved values for this field code
	// Search in JSON field_values column
	$sql = "SELECT DISTINCT ";
	$sql .= "JSON_UNQUOTE(JSON_EXTRACT(field_values, '$.\"".($db->escape($fieldCode))."\"')) as field_value ";
	$sql .= "FROM ".MAIN_DB_PREFIX."kundenkarte_anlage ";
	$sql .= "WHERE field_values IS NOT NULL ";
	$sql .= "AND JSON_EXTRACT(field_values, '$.\"".($db->escape($fieldCode))."\"') IS NOT NULL ";

	// Filter by query if provided
	if (!empty($query)) {
		$sql .= "AND JSON_UNQUOTE(JSON_EXTRACT(field_values, '$.\"".($db->escape($fieldCode))."\"')) LIKE '%".$db->escape($query)."%' ";
	}

	// Optionally filter by type
	if ($typeId > 0) {
		$sql .= "AND fk_anlage_type = ".((int) $typeId)." ";
	}

	$sql .= "ORDER BY field_value ASC ";
	$sql .= "LIMIT 20";

	$resql = $db->query($sql);
	if ($resql) {
		while ($obj = $db->fetch_object($resql)) {
			if (!empty($obj->field_value) && $obj->field_value !== 'null') {
				$suggestions[] = $obj->field_value;
			}
		}
		$db->free($resql);
	}

	echo json_encode(array('suggestions' => $suggestions));
	exit;
}

// Get all autocomplete-enabled fields for a type
if ($action == 'get_autocomplete_fields') {
	$fields = array();

	if ($typeId > 0) {
		$sql = "SELECT field_code, field_label FROM ".MAIN_DB_PREFIX."kundenkarte_anlage_type_field ";
		$sql .= "WHERE fk_anlage_type = ".((int) $typeId)." ";
		$sql .= "AND enable_autocomplete = 1 ";
		$sql .= "AND active = 1 ";
		$sql .= "AND field_type IN ('text', 'textarea')";

		$resql = $db->query($sql);
		if ($resql) {
			while ($obj = $db->fetch_object($resql)) {
				$fields[] = array(
					'code' => $obj->field_code,
					'label' => $obj->field_label
				);
			}
			$db->free($resql);
		}
	}

	echo json_encode(array('fields' => $fields));
	exit;
}

echo json_encode(array('error' => 'Unknown action'));
