<?php

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

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

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

class AlsendoRegion extends Model {

    public function __construct(Registry $registry) {
        parent::__construct($registry);
    }

    public function getCurrentRegion(): ?AlsendoRegionEntity {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "alsendo_region` WHERE `active` = 1 LIMIT 1");

        if ($query->num_rows) {
            return AlsendoRegionEntity::fromArray([
                'id' => (int)$query->row['id'],
                'name' => $query->row['name'],
                'code' => $query->row['code'],
                'active' => (bool)$query->row['active'],
                'country_id' => (int)$query->row['country_id'],
                'currency_id' => (int)$query->row['currency_id'],
            ]);
        }

        return null;
    }

    public function getRegionById(int $id): ?AlsendoRegionEntity {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "alsendo_region` WHERE `id` = '" . (int)$id . "' LIMIT 1");

        if ($query->num_rows) {
            return AlsendoRegionEntity::fromArray([
                'id' => (int)$query->row['id'],
                'name' => $query->row['name'],
                'code' => $query->row['code'],
                'active' => (bool)$query->row['active'],
                'country_id' => (int)$query->row['country_id'],
                'currency_id' => (int)$query->row['currency_id'],
            ]);
        }

        return null;
    }
}