<?php
/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License version 3.0
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

use PsCheckout\Core\WebhookDispatcher\Action\CheckPSLSignatureAction;
use PsCheckout\Core\WebhookDispatcher\Action\VerifyWebhookAction;
use PsCheckout\Core\WebhookDispatcher\Processor\DispatchWebhookProcessor;
use PsCheckout\Core\WebhookDispatcher\Provider\WebhookHeaderProvider;
use PsCheckout\Core\WebhookDispatcher\Validator\BodyValuesValidator;
use PsCheckout\Core\WebhookDispatcher\Validator\HeaderValuesValidator;
use PsCheckout\Core\WebhookDispatcher\Validator\WebhookShopIdValidator;
use PsCheckout\Core\WebhookDispatcher\ValueObject\DispatchWebhookRequest;
use PsCheckout\Core\Webhook\Service\WebhookOrderIdResolver;
use PsCheckout\Infrastructure\Controller\AbstractFrontController;
use Psr\Log\LoggerInterface;

class ps_checkoutDispatchWebHookModuleFrontController extends AbstractFrontController
{
    /**
     * @var bool If set to true, will be redirected to authentication page
     */
    public $auth = false;

    /**
     * @return bool
     *
     * @throws \PsCheckout\Core\Exception\PsCheckoutException
     */
    public function display(): bool
    {
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
            $this->exitWithResponse([
                'httpCode' => 405,
                'body' => 'Method Not Allowed',
            ]);
        }

        /** @var LoggerInterface $logger */
        $logger = $this->module->getService(LoggerInterface::class);

        $logger->info('Webhook dispatch initiated');

        try {
            /** @var WebhookHeaderProvider $headerProvider */
            $headerProvider = $this->module->getService(WebhookHeaderProvider::class);
            /** @var HeaderValuesValidator $headerValuesValidator */
            $headerValuesValidator = $this->module->getService(HeaderValuesValidator::class);

            $headerValues = $headerValuesValidator->validate();
            $logger->info('Headers validated', $headerValues);

            $isSvixWebhook = isset($headerValues['Svix-Id']);

            /** @var BodyValuesValidator $bodyValuesValidator */
            $bodyValuesValidator = $this->module->getService(BodyValuesValidator::class);

            if ($isSvixWebhook) {
                $bodyValues = $bodyValuesValidator->validate();
                $logger->info('Body validated', $bodyValues);

                /** @var VerifyWebhookAction $verifyWebhookAction */
                $verifyWebhookAction = $this->module->getService(VerifyWebhookAction::class);
                $verifyWebhookAction->execute(file_get_contents('php://input'), $headerValues);
                $logger->info('Webhook Signature validated', $bodyValues);

                /** @var WebhookOrderIdResolver $orderIdResolver */
                $orderIdResolver = $this->module->getService(WebhookOrderIdResolver::class);
                $bodyValues['orderId'] = $orderIdResolver->resolve($bodyValues);

                $dispatchWebhookRequest = DispatchWebhookRequest::createFromRequest($bodyValues);
            } else {
                $bodyValues = $bodyValuesValidator->validateMaasland();
                $logger->info('Body validated', $bodyValues);

                /** @var CheckPSLSignatureAction $checkPSLSignatureAction */
                $checkPSLSignatureAction = $this->module->getService(CheckPSLSignatureAction::class);
                $checkPSLSignatureAction->execute($bodyValues);
                $logger->info('PSL Signature validated', $bodyValues);

                $dispatchWebhookRequest = DispatchWebhookRequest::createFromMaaslandRequest($bodyValues, $headerValues);
            }

            /** @var WebhookShopIdValidator $webhookShopIdValidator */
            $webhookShopIdValidator = $this->module->getService(WebhookShopIdValidator::class);
            $webhookShopIdValidator->validate($dispatchWebhookRequest->getShopId());

            $logger->info('Webhook dispatch started');

            /** @var DispatchWebhookProcessor $dispatchWebHookProcessor */
            $dispatchWebHookProcessor = $this->module->getService(DispatchWebhookProcessor::class);

            $processed = $dispatchWebHookProcessor->process($dispatchWebhookRequest);

            if ($processed) {
                $logger->info('Webhook dispatch completed successfully');
            } else {
                $logger->warning('Webhook dispatch completed with no processing');
            }

            return $processed;
        } catch (Exception $e) {
            // Handle the exception
            $logger->error('Webhook Dispatcher error', [
                'message' => $e->getMessage(),
                'headers' => $headerProvider->getHeaders(),
                'trace' => $e->getTraceAsString(),
            ]);
            http_response_code(400);

            echo json_encode([
                'error' => $e->getMessage(),
                'code' => $e->getCode(),
            ]);
        } catch (Throwable $e) {
            $logger->error(
                sprintf(
                    'DispatchWebHookController - Exception %s : %s',
                    $e->getCode(),
                    $e->getMessage()
                ),
                ['exception' => $e]
            );
            http_response_code(500);

            echo json_encode(['error' => 'An unexpected error occurred.']);
        }

        return false;
    }

    /**
     * Override displayMaintenancePage to prevent the maintenance page to be displayed
     *
     * @see FrontController::displayMaintenancePage()
     */
    protected function displayMaintenancePage()
    {
        return;
    }

    /**
     * Override displayRestrictedCountryPage to prevent page country is not allowed
     *
     * @see FrontController::displayRestrictedCountryPage()
     */
    protected function displayRestrictedCountryPage()
    {
        return;
    }

    /**
     * Override geolocationManagement to prevent country GEOIP blocking
     *
     * @see FrontController::geolocationManagement()
     *
     * @param Country $defaultCountry
     *
     * @return false
     */
    protected function geolocationManagement($defaultCountry)
    {
        return false;
    }

    /**
     * Override sslRedirection to prevent redirection
     *
     * @see FrontController::sslRedirection()
     */
    protected function sslRedirection()
    {
        return;
    }

    /**
     * Override canonicalRedirection to prevent redirection
     *
     * @see FrontController::canonicalRedirection()
     *
     * @param string $canonical_url
     */
    protected function canonicalRedirection($canonical_url = '')
    {
        return;
    }
}
