<?php

namespace Tests\Feature\Profile;

use App\Models\Package;
use App\Models\PackagePurchase;
use App\Models\Tenant;
use App\Models\TenantPackage;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;

class ProfilePageTest extends TestCase
{
    use RefreshDatabase;

    public function test_profile_page_displays_user_and_package_information(): void
    {
        $tenant = Tenant::factory()->create([
            'subscription_status' => 'active',
            'subscription_expires_at' => now()->addMonths(3),
        ]);

        $package = Package::factory()->reseller()->create([
            'name_translations' => [
                'de' => 'Business Paket',
                'en' => 'Business Package',
            ],
        ]);

        TenantPackage::factory()->create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'price' => 199.00,
            'purchased_at' => now()->subWeek(),
            'expires_at' => now()->addMonths(3),
            'used_events' => 1,
            'active' => true,
        ]);

        PackagePurchase::factory()->create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'price' => 199.00,
            'type' => 'reseller_subscription',
            'provider' => 'paddle',
            'purchased_at' => now()->subWeek(),
        ]);

        $user = User::factory()->unverified()->create([
            'tenant_id' => $tenant->id,
            'role' => 'tenant_admin',
            'name' => 'Alex Beispiel',
            'email' => 'alex@example.test',
            'preferred_locale' => 'de',
        ]);

        $this->actingAs($user);

        $response = $this->get(route('profile.index'));

        $response->assertStatus(200)
            ->assertInertia(fn (AssertableInertia $page) => $page
                ->component('Profile/Index')
                ->where('userData.email', 'alex@example.test')
                ->where('userData.mustVerifyEmail', true)
                ->where('tenant.activePackage.name', 'Business Paket')
                ->has('purchases', fn (AssertableInertia $purchases) => $purchases
                    ->where('0.packageName', 'Business Paket')
                    ->etc()
                )
            );
    }

    public function test_localized_profile_route_resolves_to_profile_page(): void
    {
        $user = User::factory()->create([
            'preferred_locale' => 'de',
        ]);

        $this->actingAs($user);

        $response = $this->get(route('marketing.profile.index', ['locale' => 'de']));

        $response->assertStatus(200)
            ->assertInertia(fn (AssertableInertia $page) => $page->component('Profile/Index'));
    }
}
