<?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\Front;

use Customer;

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

/**
 * Decides whether the cart/checkout should offer Simply.IN 1-click to a shopper who signed into
 * the shop with the shop's own password (typically a password manager autofilled and submitted it
 * before anything else could happen).
 *
 * Such a shopper is otherwise lost in practice: the Simply.IN entry points they can reach are
 * either driven by the e-mail field a shop-authenticated shopper never fills in, or an unlabelled
 * status badge beside their identity summary — nothing that reads as "get 1-click here". So their
 * order completes through the native checkout, without a Simply.IN authorization, and never
 * reaches Simply.IN at all. Offering the authorization explicitly is what recovers the transaction.
 *
 * Whether the offer can be SHOWN is intentionally not merchant-configurable: 1-click for a
 * returning shopper is the product itself, not a display option.
 */
final class OneClickOffer
{
    /**
     * Pure decision, so the rule is stated in one testable place and the caller supplies the
     * recognition result — keeping this free of the platform's cache/session plumbing is what
     * makes the rule portable to the other shop platforms.
     *
     * @param bool $hasSimplyInAccount       result of the existing account-recognition lookup for
     *                                       this shopper's e-mail; do NOT add a second lookup here
     * @param bool $hasSimplyInAuthorization whether this session already holds a Simply.IN
     *                                       checkout authorization
     */
    public static function isAvailable(
        ?Customer $customer,
        bool $hasSimplyInAccount,
        bool $hasSimplyInAuthorization,
    ): bool {
        // Already authorized: 1-click is running, there is nothing to offer.
        if ($hasSimplyInAuthorization) {
            return false;
        }
        // Only a shopper the SHOP considers authenticated is in the situation this offer exists
        // for — a visitor still on the e-mail field is served by the existing e-mail-driven flow.
        // The guest check is stated explicitly rather than relying on Customer::isLogged() also
        // rejecting guests: a guest record is created by our own checkout flow, not by a native
        // shop login, and that distinction has to survive being ported to another platform.
        if ($customer === null || !$customer->isLogged() || $customer->isGuest()) {
            return false;
        }
        if (trim((string) $customer->email) === '') {
            return false;
        }

        // No linked account means nothing to authorize against, so nothing may be rendered — a
        // button that opens an empty popup is worse than no button.
        return $hasSimplyInAccount;
    }
}
