<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

/**
 * Class EquipmentType
 * Manages equipment type templates (Hutschienen-Komponenten)
 */
class EquipmentType extends CommonObject
{
	public $element = 'equipmenttype';
	public $table_element = 'kundenkarte_equipment_type';

	public $ref;
	public $label;
	public $label_short;
	public $description;
	public $fk_system;

	// Equipment-spezifische Felder
	public $width_te = 1;
	public $color;
	public $fk_product;
	public $terminals_config; // JSON config for terminals
	public $flow_direction; // Flussrichtung: NULL=keine, top_to_bottom, bottom_to_top
	public $terminal_position = 'both'; // Terminal-Position: both, top_only, bottom_only

	public $picto;
	public $icon_file; // Uploaded SVG/PNG file for schematic symbol (PDF)
	public $block_image; // Uploaded image for block display in SchematicEditor
	public $is_system;
	public $position;
	public $active;

	public $date_creation;
	public $fk_user_creat;
	public $fk_user_modif;

	// Loaded objects
	public $system_label;
	public $system_code;
	public $product; // Linked product object
	public $fields = array();

	/**
	 * Constructor
	 *
	 * @param DoliDB $db Database handler
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}

	/**
	 * Create object in database
	 *
	 * @param User $user User that creates
	 * @return int Return integer <0 if KO, Id of created object if OK
	 */
	public function create($user)
	{
		global $conf;

		$error = 0;
		$now = dol_now();

		if (empty($this->ref) || empty($this->label) || empty($this->fk_system)) {
			$this->error = 'ErrorMissingParameters';
			return -1;
		}

		$this->db->begin();

		$sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
		$sql .= "entity, ref, label, label_short, description, fk_system,";
		$sql .= " width_te, color, fk_product, terminals_config, flow_direction, terminal_position,";
		$sql .= " picto, icon_file, block_image, is_system, position, active,";
		$sql .= " date_creation, fk_user_creat";
		$sql .= ") VALUES (";
		$sql .= "0"; // entity 0 = global
		$sql .= ", '".$this->db->escape($this->ref)."'";
		$sql .= ", '".$this->db->escape($this->label)."'";
		$sql .= ", ".($this->label_short ? "'".$this->db->escape($this->label_short)."'" : "NULL");
		$sql .= ", ".($this->description ? "'".$this->db->escape($this->description)."'" : "NULL");
		$sql .= ", ".((int) $this->fk_system);
		$sql .= ", ".((int) ($this->width_te > 0 ? $this->width_te : 1));
		$sql .= ", ".($this->color ? "'".$this->db->escape($this->color)."'" : "NULL");
		$sql .= ", ".($this->fk_product > 0 ? ((int) $this->fk_product) : "NULL");
		$sql .= ", ".($this->terminals_config ? "'".$this->db->escape($this->terminals_config)."'" : "NULL");
		$sql .= ", ".($this->flow_direction ? "'".$this->db->escape($this->flow_direction)."'" : "NULL");
		$sql .= ", '".$this->db->escape($this->terminal_position ?: 'both')."'";
		$sql .= ", ".($this->picto ? "'".$this->db->escape($this->picto)."'" : "NULL");
		$sql .= ", ".($this->icon_file ? "'".$this->db->escape($this->icon_file)."'" : "NULL");
		$sql .= ", ".($this->block_image ? "'".$this->db->escape($this->block_image)."'" : "NULL");
		$sql .= ", 0"; // is_system = 0 for user-created
		$sql .= ", ".((int) $this->position);
		$sql .= ", ".((int) ($this->active !== null ? $this->active : 1));
		$sql .= ", '".$this->db->idate($now)."'";
		$sql .= ", ".((int) $user->id);
		$sql .= ")";

		$resql = $this->db->query($sql);
		if (!$resql) {
			$error++;
			$this->errors[] = "Error ".$this->db->lasterror();
		}

		if (!$error) {
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
		}

		if ($error) {
			$this->db->rollback();
			return -1 * $error;
		} else {
			$this->db->commit();
			return $this->id;
		}
	}

	/**
	 * Load object from database
	 *
	 * @param int $id ID of record
	 * @return int Return integer <0 if KO, 0 if not found, >0 if OK
	 */
	public function fetch($id)
	{
		$sql = "SELECT t.*, s.label as system_label, s.code as system_code,";
		$sql .= " p.ref as product_ref, p.label as product_label";
		$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system as s ON t.fk_system = s.rowid";
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON t.fk_product = p.rowid";
		$sql .= " WHERE t.rowid = ".((int) $id);

		$resql = $this->db->query($sql);
		if ($resql) {
			if ($this->db->num_rows($resql)) {
				$obj = $this->db->fetch_object($resql);

				$this->id = $obj->rowid;
				$this->entity = $obj->entity;
				$this->ref = $obj->ref;
				$this->label = $obj->label;
				$this->label_short = $obj->label_short;
				$this->description = $obj->description;
				$this->fk_system = $obj->fk_system;
				$this->width_te = $obj->width_te;
				$this->color = $obj->color;
				$this->fk_product = $obj->fk_product;
				$this->terminals_config = $obj->terminals_config;
				$this->flow_direction = $obj->flow_direction;
				$this->terminal_position = $obj->terminal_position ?: 'both';
				$this->picto = $obj->picto;
				$this->icon_file = $obj->icon_file;
				$this->block_image = $obj->block_image;
				$this->is_system = $obj->is_system;
				$this->position = $obj->position;
				$this->active = $obj->active;
				$this->date_creation = $this->db->jdate($obj->date_creation);
				$this->fk_user_creat = $obj->fk_user_creat;
				$this->fk_user_modif = $obj->fk_user_modif;

				$this->system_label = $obj->system_label;
				$this->system_code = $obj->system_code;
				$this->product_ref = $obj->product_ref;
				$this->product_label = $obj->product_label;

				$this->db->free($resql);
				return 1;
			} else {
				$this->db->free($resql);
				return 0;
			}
		} else {
			$this->error = $this->db->lasterror();
			return -1;
		}
	}

	/**
	 * Update object in database
	 *
	 * @param User $user User that modifies
	 * @return int Return integer <0 if KO, >0 if OK
	 */
	public function update($user)
	{
		$error = 0;

		$this->db->begin();

		$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
		$sql .= " ref = '".$this->db->escape($this->ref)."'";
		$sql .= ", label = '".$this->db->escape($this->label)."'";
		$sql .= ", label_short = ".($this->label_short ? "'".$this->db->escape($this->label_short)."'" : "NULL");
		$sql .= ", description = ".($this->description ? "'".$this->db->escape($this->description)."'" : "NULL");
		$sql .= ", fk_system = ".((int) $this->fk_system);
		$sql .= ", width_te = ".((int) ($this->width_te > 0 ? $this->width_te : 1));
		$sql .= ", color = ".($this->color ? "'".$this->db->escape($this->color)."'" : "NULL");
		$sql .= ", fk_product = ".($this->fk_product > 0 ? ((int) $this->fk_product) : "NULL");
		$sql .= ", terminals_config = ".($this->terminals_config ? "'".$this->db->escape($this->terminals_config)."'" : "NULL");
		$sql .= ", flow_direction = ".($this->flow_direction ? "'".$this->db->escape($this->flow_direction)."'" : "NULL");
		$sql .= ", terminal_position = '".$this->db->escape($this->terminal_position ?: 'both')."'";
		$sql .= ", picto = ".($this->picto ? "'".$this->db->escape($this->picto)."'" : "NULL");
		$sql .= ", icon_file = ".($this->icon_file ? "'".$this->db->escape($this->icon_file)."'" : "NULL");
		$sql .= ", block_image = ".($this->block_image ? "'".$this->db->escape($this->block_image)."'" : "NULL");
		$sql .= ", position = ".((int) $this->position);
		$sql .= ", active = ".((int) $this->active);
		$sql .= ", fk_user_modif = ".((int) $user->id);
		$sql .= " WHERE rowid = ".((int) $this->id);

		$resql = $this->db->query($sql);
		if (!$resql) {
			$error++;
			$this->errors[] = "Error ".$this->db->lasterror();
		}

		if ($error) {
			$this->db->rollback();
			return -1 * $error;
		} else {
			$this->db->commit();
			return 1;
		}
	}

	/**
	 * Delete object in database
	 *
	 * @param User $user User that deletes
	 * @return int Return integer <0 if KO, >0 if OK
	 */
	public function delete($user)
	{
		global $conf;

		// Check if type is in use
		$sql = "SELECT COUNT(*) as cnt FROM ".MAIN_DB_PREFIX."kundenkarte_equipment";
		$sql .= " WHERE fk_equipment_type = ".((int) $this->id);
		$resql = $this->db->query($sql);
		if ($resql) {
			$obj = $this->db->fetch_object($resql);
			if ($obj->cnt > 0) {
				$this->error = 'ErrorTypeInUse';
				return -1;
			}
		}

		// Cannot delete system types
		if ($this->is_system) {
			$this->error = 'ErrorCannotDeleteSystemType';
			return -2;
		}

		$error = 0;
		$this->db->begin();

		// Delete fields first
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field WHERE fk_equipment_type = ".((int) $this->id);
		$this->db->query($sql);

		// Delete type
		$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
		$resql = $this->db->query($sql);
		if (!$resql) {
			$error++;
			$this->errors[] = "Error ".$this->db->lasterror();
		}

		if ($error) {
			$this->db->rollback();
			return -1 * $error;
		} else {
			$this->db->commit();
			return 1;
		}
	}

	/**
	 * Fetch all equipment types for a system
	 *
	 * @param int $systemId System ID (0 = all)
	 * @param int $activeOnly Only active types
	 * @return array Array of EquipmentType objects
	 */
	public function fetchAllBySystem($systemId = 0, $activeOnly = 1)
	{
		$results = array();

		$sql = "SELECT t.*, s.label as system_label, s.code as system_code";
		$sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_kundenkarte_anlage_system as s ON t.fk_system = s.rowid";
		$sql .= " WHERE 1 = 1";
		if ($systemId > 0) {
			$sql .= " AND t.fk_system = ".((int) $systemId);
		}
		if ($activeOnly) {
			$sql .= " AND t.active = 1";
		}
		$sql .= " ORDER BY t.fk_system ASC, t.position ASC, t.label ASC";

		$resql = $this->db->query($sql);
		if ($resql) {
			while ($obj = $this->db->fetch_object($resql)) {
				$type = new EquipmentType($this->db);
				$type->id = $obj->rowid;
				$type->ref = $obj->ref;
				$type->label = $obj->label;
				$type->label_short = $obj->label_short;
				$type->fk_system = $obj->fk_system;
				$type->width_te = $obj->width_te;
				$type->color = $obj->color;
				$type->fk_product = $obj->fk_product;
				$type->flow_direction = $obj->flow_direction;
				$type->terminal_position = $obj->terminal_position ?: 'both';
				$type->picto = $obj->picto;
				$type->icon_file = $obj->icon_file;
				$type->block_image = $obj->block_image;
				$type->is_system = $obj->is_system;
				$type->position = $obj->position;
				$type->active = $obj->active;
				$type->system_label = $obj->system_label;
				$type->system_code = $obj->system_code;

				$results[] = $type;
			}
			$this->db->free($resql);
		}

		return $results;
	}

	/**
	 * Fetch fields for this equipment type
	 *
	 * @param int $activeOnly Only active fields
	 * @return array Array of field objects
	 */
	public function fetchFields($activeOnly = 1)
	{
		$results = array();

		$sql = "SELECT * FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field";
		$sql .= " WHERE fk_equipment_type = ".((int) $this->id);
		if ($activeOnly) {
			$sql .= " AND active = 1";
		}
		$sql .= " ORDER BY position ASC";

		$resql = $this->db->query($sql);
		if ($resql) {
			while ($obj = $this->db->fetch_object($resql)) {
				$results[] = $obj;
			}
			$this->db->free($resql);
		}

		$this->fields = $results;
		return $results;
	}

	/**
	 * Get fields that should be shown on the SVG block
	 *
	 * @return array Array of field objects with show_on_block = 1
	 */
	public function getBlockFields()
	{
		$results = array();

		$sql = "SELECT * FROM ".MAIN_DB_PREFIX."kundenkarte_equipment_type_field";
		$sql .= " WHERE fk_equipment_type = ".((int) $this->id);
		$sql .= " AND show_on_block = 1";
		$sql .= " AND active = 1";
		$sql .= " ORDER BY position ASC";

		$resql = $this->db->query($sql);
		if ($resql) {
			while ($obj = $this->db->fetch_object($resql)) {
				$results[] = $obj;
			}
			$this->db->free($resql);
		}

		return $results;
	}
}
