<?php
/* Copyright (C) 2026 Alles Watt lauft
 *
 * AJAX endpoint for Audit Log
 */

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.'/core/lib/date.lib.php';
dol_include_once('/kundenkarte/class/auditlog.class.php');

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

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

$action = GETPOST('action', 'aZ09');
$objectType = GETPOST('object_type', 'aZ09');
$objectId = GETPOSTINT('object_id');
$anlageId = GETPOSTINT('anlage_id');
$socid = GETPOSTINT('socid');
$limit = GETPOSTINT('limit') ?: 50;

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

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

$auditLog = new AuditLog($db);

switch ($action) {
	case 'fetch_object':
		// Fetch logs for a specific object
		if (empty($objectType) || $objectId <= 0) {
			$response['error'] = $langs->trans('ErrorMissingParameters');
			break;
		}

		$logs = $auditLog->fetchByObject($objectType, $objectId, $limit);

		$response['success'] = true;
		$response['logs'] = array();

		foreach ($logs as $log) {
			$response['logs'][] = array(
				'id' => $log->id,
				'object_type' => $log->object_type,
				'object_type_label' => $log->getObjectTypeLabel(),
				'object_id' => $log->object_id,
				'object_ref' => $log->object_ref,
				'action' => $log->action,
				'action_label' => $log->getActionLabel(),
				'action_icon' => $log->getActionIcon(),
				'action_color' => $log->getActionColor(),
				'field_changed' => $log->field_changed,
				'old_value' => $log->old_value,
				'new_value' => $log->new_value,
				'user_login' => $log->user_login,
				'user_name' => $log->user_name ?: $log->user_login,
				'date_action' => dol_print_date($log->date_action, 'dayhour'),
				'timestamp' => $log->date_action,
				'note' => $log->note
			);
		}
		break;

	case 'fetch_anlage':
		// Fetch logs for an Anlage
		if ($anlageId <= 0) {
			$response['error'] = $langs->trans('ErrorMissingParameters');
			break;
		}

		$logs = $auditLog->fetchByAnlage($anlageId, $limit);

		$response['success'] = true;
		$response['logs'] = array();

		foreach ($logs as $log) {
			$response['logs'][] = array(
				'id' => $log->id,
				'object_type' => $log->object_type,
				'object_type_label' => $log->getObjectTypeLabel(),
				'object_id' => $log->object_id,
				'object_ref' => $log->object_ref,
				'action' => $log->action,
				'action_label' => $log->getActionLabel(),
				'action_icon' => $log->getActionIcon(),
				'action_color' => $log->getActionColor(),
				'field_changed' => $log->field_changed,
				'old_value' => $log->old_value,
				'new_value' => $log->new_value,
				'user_login' => $log->user_login,
				'user_name' => $log->user_name ?: $log->user_login,
				'date_action' => dol_print_date($log->date_action, 'dayhour'),
				'timestamp' => $log->date_action,
				'note' => $log->note
			);
		}
		break;

	case 'fetch_societe':
		// Fetch logs for a customer
		if ($socid <= 0) {
			$response['error'] = $langs->trans('ErrorMissingParameters');
			break;
		}

		$logs = $auditLog->fetchBySociete($socid, $limit);

		$response['success'] = true;
		$response['logs'] = array();

		foreach ($logs as $log) {
			$response['logs'][] = array(
				'id' => $log->id,
				'object_type' => $log->object_type,
				'object_type_label' => $log->getObjectTypeLabel(),
				'object_id' => $log->object_id,
				'object_ref' => $log->object_ref,
				'fk_anlage' => $log->fk_anlage,
				'action' => $log->action,
				'action_label' => $log->getActionLabel(),
				'action_icon' => $log->getActionIcon(),
				'action_color' => $log->getActionColor(),
				'field_changed' => $log->field_changed,
				'old_value' => $log->old_value,
				'new_value' => $log->new_value,
				'user_login' => $log->user_login,
				'user_name' => $log->user_name ?: $log->user_login,
				'date_action' => dol_print_date($log->date_action, 'dayhour'),
				'timestamp' => $log->date_action,
				'note' => $log->note
			);
		}
		break;

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

echo json_encode($response);
