<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\Webhook;

use Ferienpass\CoreBundle\Webhook\PmPaymentNotifyEvent;
use Ferienpass\CoreBundle\Webhook\PmPaymentRequestParser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Webhook\Exception\RejectWebhookException;

final class PmPaymentRequestParserTest extends TestCase
{
    public function testParseReturnsEventForValidPayload(): void
    {
        $parser = new PmPaymentRequestParser();
        $request = Request::create('/webhook', 'POST', [], [], [], [], json_encode(['ags' => 'AGS123', 'txid' => 'TX-001', 'status' => 1]));
        $request->headers->set('Content-Type', 'application/json');

        $event = $parser->parse($request, 'secret');

        self::assertInstanceOf(PmPaymentNotifyEvent::class, $event);
        self::assertSame('TX-001', $event->getId());
        self::assertSame(1, $event->getStatus());
    }

    public function testParseThrowsForMissingAgs(): void
    {
        $this->expectException(RejectWebhookException::class);

        $parser = new PmPaymentRequestParser();
        $request = Request::create('/webhook', 'POST', [], [], [], [], json_encode(['txid' => 'TX-001']));
        $request->headers->set('Content-Type', 'application/json');

        $parser->parse($request, 'secret');
    }

    public function testParseThrowsForMissingTxid(): void
    {
        $this->expectException(RejectWebhookException::class);

        $parser = new PmPaymentRequestParser();
        $request = Request::create('/webhook', 'POST', [], [], [], [], json_encode(['ags' => 'AGS123']));
        $request->headers->set('Content-Type', 'application/json');

        $parser->parse($request, 'secret');
    }

    public function testParseThrowsForNonPostRequest(): void
    {
        $this->expectException(RejectWebhookException::class);

        $parser = new PmPaymentRequestParser();
        $request = Request::create('/webhook', 'GET');

        $parser->parse($request, 'secret');
    }
}
