<?php

namespace Opencart\Admin\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);
    }

    /**
     * @return LogEntity[]
     */
    public function getLogs(): array {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "alsendo_logs` ORDER BY `id` DESC");

        $logs = [];
        foreach ($query->rows as $row) {
            $log = LogEntity::fromArray([
                'id' => $row['id'],
                'entityType' => $row['entity_type'] ?? null,
                'entityId' => $row['entity_id'] ?? null,
                'logType' => $row['log_type'],
                'data' => $row['data']
            ]);
            $logs[] = $log;
        }

        return $logs;
    }
}