<?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\AbstractOrderDetailsSenderEntity;
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 AlsendoOrderDetailsSender extends Model
{
    public function __construct(Registry $registry)
    {
        parent::__construct($registry);
    }

    public function insertSender(int $orderDetailsId, array $data): void
    {
        $shippingPoint = array_key_exists('shipping_point', $data) ? (is_array($data['shipping_point']) ? json_encode($data['shipping_point']) : $data['shipping_point']) : null;
        $sqlVal = function($key) use ($data) {
            return array_key_exists($key, $data) ? ($data[$key] === null ? "NULL" : "'" . $this->db->escape($data[$key]) . "'") : "NULL";
        };
        $this->db->query("
            INSERT INTO `" . DB_PREFIX . "alsendo_order_details_sender`
            SET `order_details_id` = '" . (int)$orderDetailsId . "',
                `sender_template_id` = " . $sqlVal('sender_template_id') . ",
                `external_id` = " . $sqlVal('external_id') . ",
                `address_type` = " . $sqlVal('address_type') . ",
                `full_name` = " . $sqlVal('full_name') . ",
                `company_name` = " . $sqlVal('company_name') . ",
                `email` = " . $sqlVal('email') . ",
                `phone_number` = " . $sqlVal('phone_number') . ",
                `bank_account_number` = " . $sqlVal('bank_account_number') . ",
                `bank_code` = " . $sqlVal('bank_code') . ",
                `additional_bank_account_number` = " . $sqlVal('additional_bank_account_number') . ",
                `country` = " . $sqlVal('country') . ",
                `city` = " . $sqlVal('city') . ",
                `postal_code` = " . $sqlVal('postal_code') . ",
                `street` = " . $sqlVal('street') . ",
                `house_number` = " . $sqlVal('house_number') . ",
                `block` = " . $sqlVal('block') . ",
                `entrance` = " . $sqlVal('entrance') . ",
                `floor` = " . $sqlVal('floor') . ",
                `apartment_number` = " . $sqlVal('apartment_number') . ",
                `shipping_point` = " . ($shippingPoint !== null ? "'" . $this->db->escape($shippingPoint) . "'" : "NULL") . "
        ");
    }

    public function getSenderByOrderDetailsId(int $orderDetailsId, ?OrderDetailsEntityFactoryInterface $factory = null): ?AbstractOrderDetailsSenderEntity
    {
        $query = $this->db->query("
            SELECT *
            FROM `" . DB_PREFIX . "alsendo_order_details_sender`
            WHERE order_details_id = '" . (int)$orderDetailsId . "'
            LIMIT 1
        ");
        if (!$query->num_rows) {
            return null;
        }
        $factory = $factory ?? new ApaczkaOrderDetailsEntityFactory();
        return $factory->createSenderFromArray($query->row);
    }
}
