<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class PaddleReturnTest extends TestCase
{
    public function test_return_redirects_to_success_url_for_completed_transaction(): void
    {
        Config::set('paddle.api_key', 'test_key');
        Config::set('paddle.base_url', 'https://paddle.test');
        Config::set('paddle.environment', 'sandbox');
        Config::set('app.url', 'https://fotospiel-app.test');

        Http::fake([
            'https://paddle.test/transactions/txn_123' => Http::response([
                'data' => [
                    'id' => 'txn_123',
                    'status' => 'completed',
                    'custom_data' => [
                        'success_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1',
                        'cancel_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos',
                    ],
                ],
            ], 200),
        ]);

        $response = $this->get('/paddle/return?_ptxn=txn_123');

        $response->assertRedirect('https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1');
    }

    public function test_return_redirects_to_cancel_url_when_not_completed(): void
    {
        Config::set('paddle.api_key', 'test_key');
        Config::set('paddle.base_url', 'https://paddle.test');
        Config::set('paddle.environment', 'sandbox');
        Config::set('app.url', 'https://fotospiel-app.test');

        Http::fake([
            'https://paddle.test/transactions/txn_456' => Http::response([
                'data' => [
                    'id' => 'txn_456',
                    'status' => 'failed',
                    'custom_data' => [
                        'success_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos?addon_success=1',
                        'cancel_url' => 'https://fotospiel-app.test/event-admin/mobile/events/slug/photos',
                    ],
                ],
            ], 200),
        ]);

        $response = $this->get('/paddle/return?_ptxn=txn_456');

        $response->assertRedirect('https://fotospiel-app.test/event-admin/mobile/events/slug/photos');
    }
}
