<?php

namespace Tests\Feature\Dashboard;

use App\Models\Event;
use App\Models\Package;
use App\Models\PackagePurchase;
use App\Models\Photo;
use App\Models\Task;
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 DashboardPageTest extends TestCase
{
    use RefreshDatabase;

    public function test_unverified_user_can_access_dashboard_with_summary_data(): void
    {
        $tenant = Tenant::factory()->create();

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

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

        $user = User::factory()
            ->unverified()
            ->create([
                'tenant_id' => $tenant->id,
                'role' => 'tenant_admin',
            ]);

        $event = Event::factory()->for($tenant)->create([
            'status' => 'published',
            'is_active' => true,
            'date' => now()->addDays(7),
            'name' => ['de' => 'Sommerfest', 'en' => 'Summer Party'],
        ]);

        $task = Task::factory()->create([
            'tenant_id' => $tenant->id,
            'is_completed' => true,
        ]);

        $event->tasks()->attach($task);

        Photo::factory()->for($event)->create([
            'tenant_id' => $tenant->id,
            'created_at' => now()->subDay(),
        ]);

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

        $this->actingAs($user);

        $response = $this->get(route('dashboard'));

        $response->assertStatus(200)
            ->assertInertia(fn (AssertableInertia $page) => $page
                ->component('dashboard')
                ->has('metrics', fn (AssertableInertia $metrics) => $metrics
                    ->where('active_events', 1)
                    ->where('total_events', 1)
                    ->where('task_progress', 100)
                    ->where('upcoming_events', 1)
                    ->where('new_photos', 1)
                    ->etc()
                )
                ->where('tenant.activePackage.name', 'Premium Paket')
                ->where('tenant.activePackage.remainingEvents', 9)
                ->has('onboarding', fn (AssertableInertia $steps) => $steps
                    ->where('0.key', 'admin_app')
                    ->where('1.done', true)
                    ->etc()
                )
                ->has('upcomingEvents', 1)
                ->has('recentPurchases', 1)
                ->where('emailVerification.mustVerify', true)
                ->where('emailVerification.verified', false)
            );
    }
}
