<?php

namespace Tests\Feature\Api\Tenant;

use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventPackageAddon;
use App\Models\Package;
use App\Models\Tenant;
use Tests\Feature\Tenant\TenantTestCase;

class BillingAddonHistoryTest extends TenantTestCase
{
    public function test_tenant_can_list_addon_history(): void
    {
        $package = Package::factory()->endcustomer()->create([
            'max_photos' => 500,
            'max_guests' => 200,
            'gallery_days' => 60,
        ]);

        $event = Event::factory()->for($this->tenant)->create([
            'name' => ['de' => 'Gala', 'en' => 'Gala'],
        ]);

        $eventPackage = EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now()->subMonth(),
            'used_photos' => 0,
            'used_guests' => 0,
            'gallery_expires_at' => now()->addDays(30),
        ]);

        $firstAddon = EventPackageAddon::create([
            'event_package_id' => $eventPackage->id,
            'event_id' => $event->id,
            'tenant_id' => $this->tenant->id,
            'addon_key' => 'extra_guests_50',
            'quantity' => 1,
            'extra_guests' => 50,
            'status' => 'completed',
            'amount' => 99.00,
            'currency' => 'EUR',
            'metadata' => ['label' => '+50 Gäste'],
            'purchased_at' => now()->subDay(),
            'receipt_payload' => ['receipt_url' => 'https://receipt.example/first'],
        ]);

        $secondAddon = EventPackageAddon::create([
            'event_package_id' => $eventPackage->id,
            'event_id' => $event->id,
            'tenant_id' => $this->tenant->id,
            'addon_key' => 'extra_photos_200',
            'quantity' => 2,
            'extra_photos' => 200,
            'extra_guests' => 0,
            'status' => 'pending',
            'amount' => 149.00,
            'currency' => 'EUR',
            'metadata' => ['label' => '+200 Fotos'],
            'purchased_at' => now(),
            'receipt_payload' => ['receipt_url' => 'https://receipt.example/second'],
        ]);

        $otherTenant = Tenant::factory()->create();
        $otherEvent = Event::factory()->for($otherTenant)->create();
        $otherPackage = EventPackage::create([
            'event_id' => $otherEvent->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now()->subWeek(),
            'used_photos' => 0,
            'used_guests' => 0,
            'gallery_expires_at' => now()->addDays(30),
        ]);

        EventPackageAddon::create([
            'event_package_id' => $otherPackage->id,
            'event_id' => $otherEvent->id,
            'tenant_id' => $otherTenant->id,
            'addon_key' => 'extra_guests_999',
            'quantity' => 1,
            'extra_guests' => 999,
            'status' => 'completed',
            'amount' => 100.00,
            'currency' => 'EUR',
            'purchased_at' => now(),
        ]);

        $response = $this->authenticatedRequest('GET', '/api/v1/tenant/billing/addons');

        $response->assertOk();
        $response->assertJsonCount(2, 'data');
        $response->assertJsonPath('meta.total', 2);
        $response->assertJsonPath('data.0.id', $secondAddon->id);
        $response->assertJsonPath('data.0.receipt_url', 'https://receipt.example/second');
        $response->assertJsonPath('data.0.event.slug', $event->slug);
        $response->assertJsonPath('data.1.id', $firstAddon->id);
    }
}
