<?php

namespace Tests\Feature;

use App\Services\Paddle\PaddleClient;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class PaddleRegisterWebhooksCommandTest extends TestCase
{
    use RefreshDatabase;

    protected function tearDown(): void
    {
        Mockery::close();

        parent::tearDown();
    }

    public function test_registers_webhook_with_configured_events(): void
    {
        config([
            'paddle.webhook_events' => ['transaction.completed', 'subscription.created'],
        ]);

        $client = Mockery::mock(PaddleClient::class);
        $client->shouldReceive('post')
            ->once()
            ->with('/notification-settings', Mockery::on(function (array $payload): bool {
                return $payload['destination'] === 'https://example.test/paddle/webhook'
                    && $payload['subscribed_events'] === ['transaction.completed', 'subscription.created']
                    && $payload['traffic_source'] === 'simulation';
            }))
            ->andReturn(['data' => ['id' => 'ntfset_123']]);

        $this->app->instance(PaddleClient::class, $client);

        $this->artisan('paddle:webhooks:register', [
            '--url' => 'https://example.test/paddle/webhook',
            '--traffic-source' => 'simulation',
        ])->assertExitCode(0);
    }
}
