<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for equipment connections (generic for all system types)
 */

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");

require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentconnection.class.php';
require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipment.class.php';

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

$action = GETPOST('action', 'aZ09');
$connectionId = GETPOSTINT('connection_id');
$carrierId = GETPOSTINT('carrier_id');
$equipmentId = GETPOSTINT('equipment_id');

$connection = new EquipmentConnection($db);

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

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

switch ($action) {
	case 'get':
		// Get single connection data
		if ($connectionId > 0 && $connection->fetch($connectionId) > 0) {
			$response['success'] = true;
			$response['connection'] = array(
				'id' => $connection->id,
				'fk_source' => $connection->fk_source,
				'source_terminal' => $connection->source_terminal,
				'fk_target' => $connection->fk_target,
				'target_terminal' => $connection->target_terminal,
				'connection_type' => $connection->connection_type,
				'color' => $connection->color,
				'output_label' => $connection->output_label,
				'medium_type' => $connection->medium_type,
				'medium_spec' => $connection->medium_spec,
				'medium_length' => $connection->medium_length,
				'is_rail' => $connection->is_rail,
				'rail_start_te' => $connection->rail_start_te,
				'rail_end_te' => $connection->rail_end_te,
				'rail_phases' => $connection->rail_phases,
				'excluded_te' => $connection->excluded_te,
				'fk_carrier' => $connection->fk_carrier,
				'position_y' => $connection->position_y,
				'source_label' => $connection->source_label,
				'target_label' => $connection->target_label
			);
		} else {
			$response['error'] = 'Connection not found';
		}
		break;

	case 'list':
		// List all connections for a carrier
		if ($carrierId > 0) {
			$connections = $connection->fetchByCarrier($carrierId);
			$result = array();
			foreach ($connections as $c) {
				$result[] = array(
					'id' => $c->id,
					'fk_source' => $c->fk_source,
					'source_terminal' => $c->source_terminal,
					'source_label' => $c->source_label,
					'source_pos' => $c->source_pos,
					'source_width' => $c->source_width,
					'fk_target' => $c->fk_target,
					'target_terminal' => $c->target_terminal,
					'target_label' => $c->target_label,
					'target_pos' => $c->target_pos,
					'connection_type' => $c->connection_type,
					'color' => $c->getColor(),
					'output_label' => $c->output_label,
					'medium_type' => $c->medium_type,
					'medium_spec' => $c->medium_spec,
					'medium_length' => $c->medium_length,
					'is_rail' => $c->is_rail,
					'rail_start_te' => $c->rail_start_te,
					'rail_end_te' => $c->rail_end_te,
					'rail_phases' => $c->rail_phases,
					'excluded_te' => $c->excluded_te,
					'position_y' => $c->position_y,
					'display_label' => $c->getDisplayLabel()
				);
			}
			$response['success'] = true;
			$response['connections'] = $result;
		} else {
			$response['error'] = 'Missing carrier_id';
		}
		break;

	case 'list_outputs':
		// List outputs for an equipment
		if ($equipmentId > 0) {
			$outputs = $connection->fetchOutputs($equipmentId);
			$result = array();
			foreach ($outputs as $c) {
				$result[] = array(
					'id' => $c->id,
					'connection_type' => $c->connection_type,
					'color' => $c->getColor(),
					'output_label' => $c->output_label,
					'medium_type' => $c->medium_type,
					'medium_spec' => $c->medium_spec,
					'medium_length' => $c->medium_length,
					'display_label' => $c->getDisplayLabel()
				);
			}
			$response['success'] = true;
			$response['outputs'] = $result;
		} else {
			$response['error'] = 'Missing equipment_id';
		}
		break;

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

		$connection->fk_source = GETPOSTINT('fk_source');
		$connection->source_terminal = GETPOST('source_terminal', 'alphanohtml') ?: 'output';
		$connection->source_terminal_id = GETPOST('source_terminal_id', 'alphanohtml');
		$connection->fk_target = GETPOSTINT('fk_target');
		$connection->target_terminal = GETPOST('target_terminal', 'alphanohtml') ?: 'input';
		$connection->target_terminal_id = GETPOST('target_terminal_id', 'alphanohtml');
		$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
		$connection->color = GETPOST('color', 'alphanohtml');
		$connection->output_label = GETPOST('output_label', 'alphanohtml');
		$connection->medium_type = GETPOST('medium_type', 'alphanohtml');
		$connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
		$connection->medium_length = GETPOST('medium_length', 'alphanohtml');
		$connection->is_rail = GETPOSTINT('is_rail');
		$connection->rail_start_te = GETPOSTINT('rail_start_te');
		$connection->rail_end_te = GETPOSTINT('rail_end_te');
		$connection->fk_carrier = $carrierId;
		$connection->position_y = GETPOSTINT('position_y');
		$connection->path_data = GETPOST('path_data', 'nohtml');

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

	case 'update':
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}
		if ($connection->fetch($connectionId) > 0) {
			// Only update fields that are actually sent (preserve existing values)
			if (GETPOSTISSET('fk_source')) $connection->fk_source = GETPOSTINT('fk_source');
			if (GETPOSTISSET('source_terminal')) $connection->source_terminal = GETPOST('source_terminal', 'alphanohtml') ?: $connection->source_terminal;
			if (GETPOSTISSET('fk_target')) $connection->fk_target = GETPOSTINT('fk_target');
			if (GETPOSTISSET('target_terminal')) $connection->target_terminal = GETPOST('target_terminal', 'alphanohtml') ?: $connection->target_terminal;
			if (GETPOSTISSET('connection_type')) $connection->connection_type = GETPOST('connection_type', 'alphanohtml');
			if (GETPOSTISSET('color')) $connection->color = GETPOST('color', 'alphanohtml');
			if (GETPOSTISSET('output_label')) $connection->output_label = GETPOST('output_label', 'alphanohtml');
			if (GETPOSTISSET('medium_type')) $connection->medium_type = GETPOST('medium_type', '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('is_rail')) $connection->is_rail = GETPOSTINT('is_rail');
			if (GETPOSTISSET('rail_start_te')) $connection->rail_start_te = GETPOSTINT('rail_start_te');
			if (GETPOSTISSET('rail_end_te')) $connection->rail_end_te = GETPOSTINT('rail_end_te');
			if (GETPOSTISSET('rail_phases')) $connection->rail_phases = GETPOST('rail_phases', 'alphanohtml');
			if (GETPOSTISSET('excluded_te')) $connection->excluded_te = GETPOST('excluded_te', 'alphanohtml');
			if (GETPOSTISSET('position_y')) $connection->position_y = GETPOSTINT('position_y');
			if (GETPOSTISSET('path_data')) $connection->path_data = GETPOST('path_data', 'nohtml');

			$result = $connection->update($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $connection->error ?: 'Update failed';
			}
		} else {
			$response['error'] = 'Connection not found';
		}
		break;

	case 'delete':
		if (!$user->hasRight('kundenkarte', 'delete')) {
			$response['error'] = 'Permission denied';
			break;
		}
		if ($connection->fetch($connectionId) > 0) {
			$result = $connection->delete($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $connection->error ?: 'Delete failed';
			}
		} else {
			$response['error'] = 'Connection not found';
		}
		break;

	case 'create_rail':
		// Create a rail/bar connection spanning multiple equipment
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$connection->is_rail = 1;
		$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
		$connection->color = GETPOST('color', 'alphanohtml');
		$connection->rail_start_te = GETPOSTINT('rail_start_te');
		$connection->rail_end_te = GETPOSTINT('rail_end_te');
		$connection->rail_phases = GETPOST('rail_phases', 'alphanohtml');
		$connection->excluded_te = GETPOST('excluded_te', 'alphanohtml');
		$connection->fk_carrier = $carrierId;
		$connection->position_y = GETPOSTINT('position_y');

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

	case 'update_rail_position':
		// Update rail/busbar start and end position (for drag & drop)
		// Also supports moving to a different carrier (different panel/hutschiene)
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}
		if ($connection->fetch($connectionId) > 0) {
			// Only allow updating rail connections
			if (!$connection->is_rail) {
				$response['error'] = 'Not a rail connection';
				break;
			}

			$connection->rail_start_te = GETPOSTINT('rail_start_te');
			$connection->rail_end_te = GETPOSTINT('rail_end_te');

			// Update carrier if provided (for moving between panels)
			if (GETPOSTISSET('carrier_id') && GETPOSTINT('carrier_id') > 0) {
				$connection->fk_carrier = GETPOSTINT('carrier_id');
			}

			$result = $connection->update($user);
			if ($result > 0) {
				$response['success'] = true;
			} else {
				$response['error'] = $connection->error ?: 'Update failed';
			}
		} else {
			$response['error'] = 'Connection not found';
		}
		break;

	case 'create_output':
		// Create an output connection
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$connection->fk_source = $equipmentId;
		$connection->source_terminal = 'output';
		$connection->fk_target = null;
		$connection->connection_type = GETPOST('connection_type', 'alphanohtml');
		$connection->color = GETPOST('color', 'alphanohtml');
		$connection->output_label = GETPOST('output_label', 'alphanohtml');
		$connection->medium_type = GETPOST('medium_type', 'alphanohtml');
		$connection->medium_spec = GETPOST('medium_spec', 'alphanohtml');
		$connection->medium_length = GETPOST('medium_length', 'alphanohtml');
		$connection->fk_carrier = $carrierId;
		$connection->position_y = 0;

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

	case 'list_all':
		// List all connections for an anlage (across all carriers)
		$anlageId = GETPOSTINT('anlage_id');
		if ($anlageId > 0) {
			// Get all carriers for this anlage
			require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentcarrier.class.php';
			$carrierObj = new EquipmentCarrier($db);
			$carriers = $carrierObj->fetchByAnlage($anlageId);
			$carrierIds = array();
			foreach ($carriers as $carrier) {
				$carrierIds[] = (int)$carrier->id;
			}

			$allConnections = array();

			if (!empty($carrierIds)) {
				// Find all connections where source OR target equipment belongs to this anlage's carriers
				// This includes connections with fk_carrier=NULL
				$sql = "SELECT DISTINCT c.*,
					se.label as source_label, se.position_te as source_pos, se.width_te as source_width,
					te.label as target_label, te.position_te as target_pos
					FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_connection c
					LEFT JOIN ".MAIN_DB_PREFIX."kundenkarte_equipment se ON c.fk_source = se.rowid
					LEFT JOIN ".MAIN_DB_PREFIX."kundenkarte_equipment te ON c.fk_target = te.rowid
					WHERE (c.fk_carrier IN (".implode(',', $carrierIds).")
					   OR se.fk_carrier IN (".implode(',', $carrierIds).")
					   OR te.fk_carrier IN (".implode(',', $carrierIds)."))
					AND c.status = 1";

				$resql = $db->query($sql);
				if ($resql) {
					while ($obj = $db->fetch_object($resql)) {
						$allConnections[] = array(
							'id' => $obj->rowid,
							'fk_source' => $obj->fk_source,
							'source_terminal' => $obj->source_terminal,
							'source_terminal_id' => $obj->source_terminal_id,
							'source_label' => $obj->source_label,
							'source_pos' => $obj->source_pos,
							'source_width' => $obj->source_width,
							'fk_target' => $obj->fk_target,
							'target_terminal' => $obj->target_terminal,
							'target_terminal_id' => $obj->target_terminal_id,
							'target_label' => $obj->target_label,
							'target_pos' => $obj->target_pos,
							'connection_type' => $obj->connection_type,
							'color' => $obj->color ?: '#3498db',
							'output_label' => $obj->output_label,
							'medium_type' => $obj->medium_type,
							'medium_spec' => $obj->medium_spec,
							'medium_length' => $obj->medium_length,
							'is_rail' => $obj->is_rail,
							'rail_start_te' => $obj->rail_start_te,
							'rail_end_te' => $obj->rail_end_te,
							'rail_phases' => $obj->rail_phases,
							'position_y' => $obj->position_y,
							'fk_carrier' => $obj->fk_carrier,
							'path_data' => isset($obj->path_data) ? $obj->path_data : null
						);
					}
					$db->free($resql);
				}
			}

			$response['success'] = true;
			$response['connections'] = $allConnections;
		} else {
			$response['error'] = 'Missing anlage_id';
		}
		break;

	case 'clear_all':
		// Delete all connections for an anlage
		if (!$user->hasRight('kundenkarte', 'delete')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$anlageId = GETPOSTINT('anlage_id');
		if ($anlageId > 0) {
			// Get all carriers for this anlage
			require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/equipmentcarrier.class.php';
			$carrierObj = new EquipmentCarrier($db);
			$carriers = $carrierObj->fetchByAnlage($anlageId);

			$deletedCount = 0;
			foreach ($carriers as $carrier) {
				$sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_connection WHERE fk_carrier = ".((int)$carrier->id);
				$resql = $db->query($sql);
				if ($resql) {
					$deletedCount += $db->affected_rows($resql);
				}
			}

			$response['success'] = true;
			$response['deleted_count'] = $deletedCount;
		} else {
			$response['error'] = 'Missing anlage_id';
		}
		break;

	// ============================================
	// Bridge Actions (Brücken zwischen Klemmen)
	// ============================================

	case 'list_bridges':
		// List all bridges for an anlage
		$anlageId = GETPOSTINT('anlage_id');
		if ($anlageId > 0) {
			require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/terminalbridge.class.php';
			$bridgeObj = new TerminalBridge($db);
			$bridges = $bridgeObj->fetchAllByAnlage($anlageId);

			$bridgeList = array();
			foreach ($bridges as $bridge) {
				$bridgeList[] = array(
					'id' => $bridge->id,
					'fk_carrier' => $bridge->fk_carrier,
					'start_te' => $bridge->start_te,
					'end_te' => $bridge->end_te,
					'terminal_side' => $bridge->terminal_side,
					'terminal_row' => $bridge->terminal_row,
					'color' => $bridge->color,
					'bridge_type' => $bridge->bridge_type,
					'label' => $bridge->label
				);
			}

			$response['success'] = true;
			$response['bridges'] = $bridgeList;
		} else {
			$response['error'] = 'Missing anlage_id';
		}
		break;

	case 'create_bridge':
		// Create a new terminal bridge
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$anlageId = GETPOSTINT('anlage_id');
		$carrierId = GETPOSTINT('carrier_id');
		$startTE = GETPOSTINT('start_te');
		$endTE = GETPOSTINT('end_te');

		if (empty($anlageId) || empty($carrierId) || empty($startTE) || empty($endTE)) {
			$response['error'] = 'Missing required parameters';
			break;
		}

		require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/terminalbridge.class.php';
		$bridge = new TerminalBridge($db);
		$bridge->fk_anlage = $anlageId;
		$bridge->fk_carrier = $carrierId;
		$bridge->start_te = min($startTE, $endTE);
		$bridge->end_te = max($startTE, $endTE);
		$bridge->terminal_side = GETPOST('terminal_side', 'alpha') ?: 'top';
		$bridge->terminal_row = GETPOSTINT('terminal_row');
		$bridge->color = GETPOST('color', 'alphanohtml') ?: '#e74c3c';
		$bridge->bridge_type = GETPOST('bridge_type', 'alpha') ?: 'standard';
		$bridge->label = GETPOST('label', 'alphanohtml');

		$result = $bridge->create($user);
		if ($result > 0) {
			$response['success'] = true;
			$response['bridge_id'] = $result;
			$response['bridge'] = array(
				'id' => $bridge->id,
				'fk_carrier' => $bridge->fk_carrier,
				'start_te' => $bridge->start_te,
				'end_te' => $bridge->end_te,
				'terminal_side' => $bridge->terminal_side,
				'terminal_row' => $bridge->terminal_row,
				'color' => $bridge->color,
				'bridge_type' => $bridge->bridge_type,
				'label' => $bridge->label
			);
		} else {
			$response['error'] = $bridge->error ?: 'Create failed';
		}
		break;

	case 'update_bridge':
		// Update an existing bridge
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$bridgeId = GETPOSTINT('bridge_id');
		if ($bridgeId > 0) {
			require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/terminalbridge.class.php';
			$bridge = new TerminalBridge($db);
			if ($bridge->fetch($bridgeId) > 0) {
				if (GETPOSTISSET('start_te')) $bridge->start_te = GETPOSTINT('start_te');
				if (GETPOSTISSET('end_te')) $bridge->end_te = GETPOSTINT('end_te');
				if (GETPOSTISSET('terminal_side')) $bridge->terminal_side = GETPOST('terminal_side', 'alpha');
				if (GETPOSTISSET('terminal_row')) $bridge->terminal_row = GETPOSTINT('terminal_row');
				if (GETPOSTISSET('color')) $bridge->color = GETPOST('color', 'alphanohtml');
				if (GETPOSTISSET('bridge_type')) $bridge->bridge_type = GETPOST('bridge_type', 'alpha');
				if (GETPOSTISSET('label')) $bridge->label = GETPOST('label', 'alphanohtml');

				$result = $bridge->update($user);
				if ($result > 0) {
					$response['success'] = true;
				} else {
					$response['error'] = $bridge->error ?: 'Update failed';
				}
			} else {
				$response['error'] = 'Bridge not found';
			}
		} else {
			$response['error'] = 'Missing bridge_id';
		}
		break;

	case 'delete_bridge':
		// Delete a bridge
		if (!$user->hasRight('kundenkarte', 'write')) {
			$response['error'] = 'Permission denied';
			break;
		}

		$bridgeId = GETPOSTINT('bridge_id');
		if ($bridgeId > 0) {
			require_once DOL_DOCUMENT_ROOT.'/custom/kundenkarte/class/terminalbridge.class.php';
			$bridge = new TerminalBridge($db);
			if ($bridge->fetch($bridgeId) > 0) {
				$result = $bridge->delete($user);
				if ($result > 0) {
					$response['success'] = true;
				} else {
					$response['error'] = $bridge->error ?: 'Delete failed';
				}
			} else {
				$response['error'] = 'Bridge not found';
			}
		} else {
			$response['error'] = 'Missing bridge_id';
		}
		break;

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

echo json_encode($response);
$db->close();
