<?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
 */

declare(strict_types=1);

namespace SimplyIn\BackOffice;

use Configuration;
use Context;
use Module;
use Order;
use SimplyInOrder;
use SimplyInPoint;
use Tools;

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

final class AdminOrderDisplay
{
    public function __construct(private readonly OrderStatusSync $orderStatusSync)
    {
    }

    /**
     * @param array<string, mixed> $params
     */
    public function onOrderStatusPostUpdate(array $params): void
    {
        if (!Configuration::get('SIMPLYIN_CONNECTED')) {
            return;
        }
        if (!isset($params['id_order'], $params['newOrderStatus'])) {
            return;
        }

        $status = (string) ($params['newOrderStatus']->template ?? '');
        if (!in_array($status, ['shipped', 'payment'], true)) {
            return;
        }

        $idOrder = (int) $params['id_order'];
        $order = SimplyInOrder::getByIdOrder($idOrder);
        if (!$order->id) {
            return;
        }

        $this->orderStatusSync->changeOrderState($idOrder, $status);
    }

    /**
     * @param array<string, mixed> $params
     */
    public function renderOrderPanel(Module $module, array $params, string $moduleFile): ?string
    {
        $idOrder = $params['id_order'] ?? null;
        if ($idOrder === null) {
            return null;
        }

        $order = new Order((int) $idOrder);
        $idCart = (int) $order->id_cart;
        if ($idCart === 0) {
            return null;
        }

        $selectedPoint = SimplyInPoint::getByIdCart($idCart);
        if (!$selectedPoint->id) {
            return null;
        }

        Context::getContext()->smarty->assign([
            'selected_point' => $selectedPoint,
            'module_path' => _MODULE_DIR_ . $module->name . '/',
        ]);

        return $module->display($moduleFile, 'views/templates/admin/order.tpl');
    }

    public function registerAdminAssets(Module $module): void
    {
        if (Tools::getValue('configure') !== $module->name) {
            return;
        }

        $controller = Context::getContext()->controller;
        if ($controller === null) {
            return;
        }

        $controller->addJqueryPlugin('select2');
        $moduleUrl = _MODULE_DIR_ . $module->name . '/';
        // Legacy addJS/addCSS have no cache-busting of their own; pin the module version so
        // browsers refetch after an update instead of serving a stale file.
        $controller->addJS($moduleUrl . 'views/js/dist/back.js?v=' . $module->version);
        $controller->addCSS($moduleUrl . 'views/css/simplyin_back.css?v=' . $module->version);
    }
}
