<?php

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

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

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

class AlsendoOrderDetails extends Model {

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

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

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

        $orderDetailsArray = $orderDetailsQuery->row;
        $this->registry->load->model('extension/alsendo/module/alsendo_region');
        $region = $this->registry->model_extension_alsendo_module_alsendo_region->getRegionById((int)$orderDetailsArray['region_id']);
        $regionFactory = new RegionOrderDetailsEntityFactory();
        $factory = $region !== null ? $regionFactory->getFactoryForRegionCode($region->code) : $regionFactory->getFactoryForRegionCode('poland');
        $orderDetails = $factory->createOrderDetailsFromArray($orderDetailsQuery->row);
        $orderDetails->region = $region;

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_sender');
        $orderDetails->sender = $this->registry->model_extension_alsendo_module_alsendo_order_details_sender->getSenderByOrderDetailsId($orderDetails->id, $factory);

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_shipping_address');
        $orderDetails->shippingAddress = $this->registry->model_extension_alsendo_module_alsendo_order_details_shipping_address->getShippingAddressByOrderDetailsId($orderDetails->id, $factory);

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_package_details');
        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_package');
        $packageDetails = $this->registry->model_extension_alsendo_module_alsendo_order_details_package_details->getPackageDetailsByOrderDetailsId($orderDetails->id, $factory);
        if ($packageDetails !== null) {
            $packageDetails->packages = $this->registry->model_extension_alsendo_module_alsendo_order_details_package->getPackagesByOrderDetailsPackageDetailsId($packageDetails->id, $factory);
        }
        $orderDetails->packageDetails = $packageDetails;

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_shipment');
        $orderDetails->shipment = $this->registry->model_extension_alsendo_module_alsendo_order_details_shipment->getShipmentByOrderDetailsId($orderDetails->id, $factory);

        return $orderDetails;
    }

    public function insertOrderDetails(int $orderId, int $regionId): int
    {
        $now = date('Y-m-d H:i:s');
        $this->db->query("
            INSERT INTO `" . DB_PREFIX . "alsendo_order_details`
            SET `order_id` = '" . (int)$orderId . "',
                `region_id` = " . ($regionId > 0 ? "'" . (int)$regionId . "'" : "NULL") . ",
                `created_at` = '" . $this->db->escape($now) . "',
                `updated_at` = NULL
        ");
        return (int)$this->db->getLastId();
    }
}