<?php

namespace Tests\Unit;

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

class PaddleCatalogServiceTest extends TestCase
{
    use RefreshDatabase;

    protected function tearDown(): void
    {
        parent::tearDown();

        Mockery::close();
    }

    public function test_build_product_payload_includes_translations_and_features(): void
    {
        $package = Package::factory()->create([
            'name' => 'Starter',
            'slug' => 'starter',
            'description' => '<p>Great package</p>',
            'name_translations' => [
                'de' => 'Starter DE',
                'en' => 'Starter EN',
            ],
            'description_translations' => [
                'de' => 'Beschreibung',
                'en' => 'Description',
            ],
            'features' => [
                'custom_domain' => true,
                'advanced_analytics' => false,
            ],
        ]);

        $service = new PaddleCatalogService(Mockery::mock(PaddleClient::class));

        $payload = $service->buildProductPayload($package);

        $this->assertSame('Starter', $payload['name']);
        $this->assertSame('Great package', $payload['description']);
        $this->assertSame('standard', $payload['tax_category']);
        $this->assertSame('standard', $payload['type']);
        $this->assertArrayHasKey('custom_data', $payload);
        $this->assertSame((string) $package->id, $payload['custom_data']['fotospiel_package_id']);
        $this->assertSame('starter', $payload['custom_data']['slug']);
        $this->assertSame(['de' => 'Starter DE', 'en' => 'Starter EN'], $payload['custom_data']['translations']['name']);
        $this->assertArrayHasKey('features', $payload['custom_data']);
    }

    public function test_build_price_payload_converts_price_and_currency(): void
    {
        $package = Package::factory()->create([
            'price' => 29.99,
            'description' => null,
            'name' => 'Silver Plan',
        ]);

        $service = new PaddleCatalogService(Mockery::mock(PaddleClient::class));

        $payload = $service->buildPricePayload($package, 'pro_123');

        $this->assertSame('pro_123', $payload['product_id']);
        $this->assertSame('2999', $payload['unit_price']['amount']);
        $this->assertSame('EUR', $payload['unit_price']['currency_code']);
        $this->assertSame('Silver Plan package', $payload['description']);
        $this->assertArrayHasKey('custom_data', $payload);
    }
}
