<?php

/**
 * Copyright 2024-2025 Simply.IN Sp. z o.o.
 *
 * NOTICE OF LICENSE
 *
 * Licensed under the EUPL-1.2 or later.
 * You may not use this work except in compliance with the Licence.
 *
 * Copy of the Licence is available at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * It is bundled with this package in the file LICENSE.txt
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Licence is distributed on an as is basis,
 * without warranties or conditions of any kind, either express or implied.
 * Check the Licence for the specific language governing permissions
 * and limitations under the License.
 *
 * @author   Simply.IN Sp. z o.o.
 * @copyright 2024-2025 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class SimplyInOrderSync extends ObjectModel
{
    use SimplyInInstallSqlTrait;

    public $id;

    public $cart_id;

    public $order_id;

    public $is_synced = false;

    public $ignored = false;

    public $date_synced = '0000-00-00 00:00:00';

    public static $token = '';

    public static $definition = [
        'table' => 'simplyin_order_sync',
        'primary' => 'id_simplyin_order_sync',
        'multilang' => false,
        'multilang_shop' => false,
        'fields' => [
            'cart_id' => [
                'type' => self::TYPE_INT,
                'size' => 100,
                'index' => true,
            ],
            'order_id' => [
                'type' => self::TYPE_INT,
                'size' => 36,
                'index' => true,
            ],
            'is_synced' => [
                'type' => self::TYPE_BOOL,
            ],
            'ignored' => [
                'type' => self::TYPE_BOOL,
            ],
            'date_synced' => [
                'type' => self::TYPE_DATE,
            ],
        ],
    ];

    public static function getByIdOrder($order_id)
    {
        $sql = '
            SELECT      *
            FROM        `' . _DB_PREFIX_ . self::$definition['table'] . '`
            WHERE       order_id = ' . (int) $order_id;
        $row = Db::getInstance()->getRow($sql);
        if ($row) {
            $object = new SimplyInOrderSync($row['id_simplyin_order_sync']);

            return $object;
        }
        $object = new SimplyInOrderSync();
        $object->order_id = $order_id;
        $order = new Order($order_id);
        $object->cart_id = $order->id_cart;

        return $object;

    }

    private static function payloadBuilder(): \SimplyIn\Checkout\OrderPayloadBuilder
    {
        return new \SimplyIn\Checkout\OrderPayloadBuilder(
            new \SimplyIn\Checkout\OrderAddressMapper(),
            new \SimplyIn\Checkout\OrderIpResolver(),
        );
    }

    public static function getOrderItemsData($order)
    {
        return self::payloadBuilder()->orderItemsData($order);
    }

    public static function buildExistingAccountOrderData(
        $order,
        $items_data,
        $payment_method,
        $plugin_version,
        $shop_version,
        $user_email,
        $parcel_machine_id,
        $parcel_provider_name = 'inpost',
    ) {
        return self::payloadBuilder()->existingAccountOrderData(
            $order,
            (array) $items_data,
            (array) $payment_method,
            (string) $plugin_version,
            (string) $shop_version,
            (string) $user_email,
            (string) $parcel_machine_id,
            (string) $parcel_provider_name,
        );
    }

    public static function getOrderData($id_order)
    {
        return self::payloadBuilder()->orderData((int) $id_order);
    }

    public static function getParcelMachineIdByOrderId($id_order)
    {
        return self::payloadBuilder()->parcelMachineId((int) $id_order);
    }

    public static function getParcelProviderByOrderId($id_order)
    {
        return self::payloadBuilder()->parcelProvider((int) $id_order);
    }
}
