<?php

namespace Tests\Unit\Jobs;

use App\Jobs\SyncPackageAddonToPaddle;
use App\Models\PackageAddon;
use App\Services\Paddle\PaddleAddonCatalogService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class SyncPackageAddonToPaddleTest extends TestCase
{
    use RefreshDatabase;

    public function test_creates_and_updates_price_and_product(): void
    {
        $addon = PackageAddon::create([
            'key' => 'extra_photos_500',
            'label' => '+500 Fotos',
            'extra_photos' => 500,
            'metadata' => ['price_eur' => 5],
        ]);

        $service = Mockery::mock(PaddleAddonCatalogService::class);
        $service->shouldReceive('createProduct')
            ->once()
            ->andReturn(['id' => 'pro_addon_1']);
        $service->shouldReceive('createPrice')
            ->once()
            ->andReturn(['id' => 'pri_addon_1']);

        $job = new SyncPackageAddonToPaddle($addon->id);
        $job->handle($service);

        $addon->refresh();

        $this->assertSame('pri_addon_1', $addon->price_id);
        $this->assertEquals('pro_addon_1', $addon->metadata['paddle_product_id']);
        $this->assertEquals('synced', $addon->metadata['paddle_sync_status']);
    }
}
