<?php

namespace Openguru\OpenCartFramework\Telegram;

use Closure;

class TelegramValidateInitDataMiddleware
{
    private SignatureValidator $signatureValidator;

    private array $excluded = [
        'testTgMessage',
        'manifest',
        'webhook',
        'health',
        'etlCustomers',
        'etlCustomersMeta',
    ];

    public function __construct(SignatureValidator $signatureValidator)
    {
        $this->signatureValidator = $signatureValidator;
    }

    public function handle($request, Closure $next)
    {
        $apiAction = $request->get('api_action');

        if (! in_array($apiAction, $this->excluded, true)) {
            $this->signatureValidator->validate($request);
        }

        return $next($request);
    }
}
