<?php

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

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

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

class AlsendoOrderPickupPoints extends Model {

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

    public function getPickupPointByOrderId(int $orderId): ?OrderPickupPointEntity
    {
        $orderDetailsQuery = $this->db->query("
            SELECT *
            FROM `" . DB_PREFIX . "alsendo_order_pickup_points`
            WHERE order_id = '" . $orderId . "'
        ");

        if (!$orderDetailsQuery->num_rows) {
            return null;
        }

        return OrderPickupPointEntity::fromArray($orderDetailsQuery->row);
    }

    public function removePickupPointDataByOrderId(int $orderId): void
    {
        $this->db->query("
            UPDATE `" . DB_PREFIX . "alsendo_order_details_shipment` op
            LEFT JOIN `" . DB_PREFIX . "alsendo_order_details` od ON od.id = op.order_details_id
            SET 
                op.shipment_pickup_point = NULL,
                op.shipment_pickup_point_display = NULL
            WHERE od.order_id = '" . $orderId . "'
        ");
    }
}