<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for anlage connections (Verbindungen zwischen Anlagen-Elementen)
 */

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/anlageconnection.class.php');
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');
$connectionId = GETPOSTINT('connection_id');
$anlageId = GETPOSTINT('anlage_id');
$socId = GETPOSTINT('soc_id');
$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;
}

$connection = new AnlageConnection($db);

switch ($action) {
	case 'list':
		// List connections for an anlage or customer
		if ($anlageId > 0) {
			$connections = $connection->fetchByAnlage($anlageId);
		} elseif ($socId > 0) {
			$connections = $connection->fetchBySociete($socId, $systemId);
		} else {
			$response['error'] = 'Missing anlage_id or soc_id';
			break;
		}

		$result = array();
		foreach ($connections as $c) {
			$result[] = array(
				'id' => $c->id,
				'fk_source' => $c->fk_source,
				'source_label' => $c->source_label,
				'source_ref' => $c->source_ref,
				'fk_target' => $c->fk_target,
				'target_label' => $c->target_label,
				'target_ref' => $c->target_ref,
				'label' => $c->label,
				'fk_medium_type' => $c->fk_medium_type,
				'medium_type_label' => $c->medium_type_label,
				'medium_type_text' => $c->medium_type_text,
				'medium_spec' => $c->medium_spec,
				'medium_length' => $c->medium_length,
				'medium_color' => $c->medium_color,
				'route_description' => $c->route_description,
				'installation_date' => $c->installation_date,
				'status' => $c->status,
				'display_label' => $c->getDisplayLabel()
			);
		}

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

	case 'get':
		// Get single connection
		if ($connectionId > 0 && $connection->fetch($connectionId) > 0) {
			$response['success'] = true;
			$response['connection'] = array(
				'id' => $connection->id,
				'fk_source' => $connection->fk_source,
				'source_label' => $connection->source_label,
				'source_ref' => $connection->source_ref,
				'fk_target' => $connection->fk_target,
				'target_label' => $connection->target_label,
				'target_ref' => $connection->target_ref,
				'label' => $connection->label,
				'fk_medium_type' => $connection->fk_medium_type,
				'medium_type_label' => $connection->medium_type_label,
				'medium_type_text' => $connection->medium_type_text,
				'medium_spec' => $connection->medium_spec,
				'medium_length' => $connection->medium_length,
				'medium_color' => $connection->medium_color,
				'route_description' => $connection->route_description,
				'installation_date' => $connection->installation_date,
				'status' => $connection->status
			);
		} else {
			$response['error'] = $langs->trans('ErrorRecordNotFound');
		}
		break;

	case 'create':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = $langs->trans('ErrorPermissionDenied');
			break;
		}

		$connection->fk_source = GETPOSTINT('fk_source');
		$connection->fk_target = GETPOSTINT('fk_target');
		$connection->label = GETPOST('label', 'alphanohtml');
		$connection->fk_medium_type = GETPOSTINT('fk_medium_type');
		$connection->medium_type_text = GETPOST('medium_type_text', 'alphanohtml');
		$connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
		$connection->medium_length = GETPOST('medium_length', 'alphanohtml');
		$connection->medium_color = GETPOST('medium_color', 'alphanohtml');
		$connection->route_description = GETPOST('route_description', 'restricthtml');
		$connection->installation_date = GETPOST('installation_date', 'alpha');
		$connection->status = 1;

		if (empty($connection->fk_source) || empty($connection->fk_target)) {
			$response['error'] = $langs->trans('ErrorFieldRequired', 'Source/Target');
			break;
		}

		$result = $connection->create($user);
		if ($result > 0) {
			$response['success'] = true;
			$response['connection_id'] = $result;
		} else {
			$response['error'] = $connection->error;
		}
		break;

	case 'update':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = $langs->trans('ErrorPermissionDenied');
			break;
		}

		if ($connection->fetch($connectionId) > 0) {
			if (GETPOSTISSET('fk_source')) $connection->fk_source = GETPOSTINT('fk_source');
			if (GETPOSTISSET('fk_target')) $connection->fk_target = GETPOSTINT('fk_target');
			if (GETPOSTISSET('label')) $connection->label = GETPOST('label', 'alphanohtml');
			if (GETPOSTISSET('fk_medium_type')) $connection->fk_medium_type = GETPOSTINT('fk_medium_type');
			if (GETPOSTISSET('medium_type_text')) $connection->medium_type_text = GETPOST('medium_type_text', 'alphanohtml');
			if (GETPOSTISSET('medium_spec')) $connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
			if (GETPOSTISSET('medium_length')) $connection->medium_length = GETPOST('medium_length', 'alphanohtml');
			if (GETPOSTISSET('medium_color')) $connection->medium_color = GETPOST('medium_color', 'alphanohtml');
			if (GETPOSTISSET('route_description')) $connection->route_description = GETPOST('route_description', 'restricthtml');
			if (GETPOSTISSET('installation_date')) $connection->installation_date = GETPOST('installation_date', 'alpha');
			if (GETPOSTISSET('status')) $connection->status = GETPOSTINT('status');

			$result = $connection->update($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $connection->error;
			}
		} else {
			$response['error'] = $langs->trans('ErrorRecordNotFound');
		}
		break;

	case 'delete':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = $langs->trans('ErrorPermissionDenied');
			break;
		}

		if ($connection->fetch($connectionId) > 0) {
			$result = $connection->delete($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $connection->error;
			}
		} else {
			$response['error'] = $langs->trans('ErrorRecordNotFound');
		}
		break;

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

echo json_encode($response);
