<?php

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

require_once(DIR_EXTENSION . 'alsendo/src/DTO/Entity/OrderDetailsEntityFactoryInterface.php');
require_once(DIR_EXTENSION . 'alsendo/src/DTO/Entity/Apaczka/ApaczkaOrderDetailsEntityFactory.php');

use Opencart\Extension\Alsendo\DTO\Entity\AbstractOrderDetailsPackageDetailsEntity;
use Opencart\Extension\Alsendo\DTO\Entity\OrderDetailsEntityFactoryInterface;
use Opencart\Extension\Alsendo\DTO\Entity\Apaczka\ApaczkaOrderDetailsEntityFactory;
use Opencart\System\Engine\Model;
use Opencart\System\Engine\Registry;

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

    public function insertPackageDetails(int $orderDetailsId, array $data): int
    {
        $this->db->query("
            INSERT INTO `" . DB_PREFIX . "alsendo_order_details_package_details`
            SET `order_details_id` = '" . (int)$orderDetailsId . "',
                `template_id` = " . (array_key_exists('template_id', $data) && $data['template_id'] !== null && $data['template_id'] !== '' ? "'" . (int)$data['template_id'] . "'" : "NULL") . ",
                `template_name` = " . (array_key_exists('template_name', $data) ? ($data['template_name'] === null ? "NULL" : "'" . $this->db->escape($data['template_name']) . "'") : "NULL") . ",
                `cod_value` = " . (array_key_exists('cod_value', $data) ? ($data['cod_value'] === null ? "NULL" : "'" . $this->db->escape((string)$data['cod_value']) . "'") : "NULL") . ",
                `declared_value` = " . (array_key_exists('declared_value', $data) ? ($data['declared_value'] === null ? "NULL" : "'" . $this->db->escape((string)$data['declared_value']) . "'") : "NULL") . ",
                `package_content` = " . (array_key_exists('package_content', $data) ? ($data['package_content'] === null ? "NULL" : "'" . $this->db->escape($data['package_content']) . "'") : "NULL") . ",
                `pickup_request` = " . (array_key_exists('pickup_request', $data) ? ($data['pickup_request'] === null ? "NULL" : "'" . $this->db->escape((string)$data['pickup_request']) . "'") : "NULL") . ",
                `pickup_request_date` = " . (array_key_exists('pickup_request_date', $data) ? ($data['pickup_request_date'] === null ? "NULL" : "'" . $this->db->escape((string)$data['pickup_request_date']) . "'") : "NULL") . ",
                `pickup_type` = " . (array_key_exists('pickup_type', $data) ? ($data['pickup_type'] === null ? "NULL" : "'" . $this->db->escape($data['pickup_type']) . "'") : "NULL") . ",
                `ship_via_pickup_point` = " . (array_key_exists('ship_via_pickup_point', $data) ? ($data['ship_via_pickup_point'] === null ? "NULL" : "'" . $this->db->escape((string)$data['ship_via_pickup_point']) . "'") : "NULL") . "
        ");
        return (int)$this->db->getLastId();
    }

    public function getPackageDetailsByOrderDetailsId(int $orderDetailsId, ?OrderDetailsEntityFactoryInterface $factory = null, string $currencyCode = 'PLN'): ?AbstractOrderDetailsPackageDetailsEntity
    {
        $query = $this->db->query("
            SELECT *
            FROM `" . DB_PREFIX . "alsendo_order_details_package_details`
            WHERE order_details_id = '" . (int)$orderDetailsId . "'
            LIMIT 1
        ");
        if (!$query->num_rows) {
            return null;
        }
        $factory = $factory ?? new ApaczkaOrderDetailsEntityFactory();
        return $factory->createPackageDetailsFromArray($query->row, $currencyCode);
    }

}
