<?php

namespace Tests\Unit;

use App\Services\Paddle\PaddleClient;
use App\Services\Paddle\PaddleTransactionService;
use Mockery;
use Tests\TestCase;

class PaddleTransactionServiceTest extends TestCase
{
    public function test_list_for_customer_uses_expected_order_by_format(): void
    {
        $client = Mockery::mock(PaddleClient::class);
        $client->shouldReceive('get')
            ->once()
            ->with('/transactions', Mockery::on(function (array $payload) {
                return $payload['customer_id'] === 'ctm_123'
                    && $payload['order_by'] === 'created_at[desc]';
            }))
            ->andReturn(['data' => [], 'meta' => ['pagination' => []]]);

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

        $service = $this->app->make(PaddleTransactionService::class);
        $service->listForCustomer('ctm_123');

        $this->assertTrue(true);
    }

    public function test_find_by_checkout_id_uses_expected_order_by_format(): void
    {
        $client = Mockery::mock(PaddleClient::class);
        $client->shouldReceive('get')
            ->once()
            ->with('/transactions', Mockery::on(function (array $payload) {
                return $payload['checkout_id'] === 'chk_123'
                    && $payload['order_by'] === 'created_at[desc]';
            }))
            ->andReturn(['data' => []]);

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

        $service = $this->app->make(PaddleTransactionService::class);
        $this->assertNull($service->findByCheckoutId('chk_123'));
    }

    public function test_find_by_custom_data_uses_expected_order_by_format(): void
    {
        $client = Mockery::mock(PaddleClient::class);
        $client->shouldReceive('get')
            ->once()
            ->with('/transactions', Mockery::on(function (array $payload) {
                return $payload['order_by'] === 'created_at[desc]'
                    && $payload['per_page'] === 20;
            }))
            ->andReturn(['data' => []]);

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

        $service = $this->app->make(PaddleTransactionService::class);
        $this->assertNull($service->findByCustomData([
            'checkout_session_id' => 'sess_123',
        ]));
    }
}
