<?php

namespace Opencart\Catalog\Model\Extension\Alsendo\Module;

require_once(DIR_EXTENSION . 'alsendo/src/DTO/Entity/LogEntity.php');

use Opencart\Extension\Alsendo\DTO\Entity\LogEntity;
use Opencart\System\Engine\Model;
use Opencart\System\Engine\Registry;

class Log extends Model {
    public function __construct(Registry $registry) {
        parent::__construct($registry);
    }

    public function saveLog(LogEntity $logEntity): void {
        $entityType = $this->db->escape($logEntity->entityType ?? '');
        $entityId = $logEntity->entityId ?? 0;
        $logType = $this->db->escape($logEntity->logType);
        $data = is_array($logEntity->data) ? json_encode($logEntity->data) : (string)$logEntity->data;

        $this->db->query(
            "INSERT INTO `" . DB_PREFIX . "alsendo_logs` 
            (`entity_type`, `entity_id`, `log_type`, `data`) 
            VALUES ('{$entityType}', {$entityId}, '{$logType}', '" . $this->db->escape($data) . "')"
        );
    }
}