<?php

namespace Tests\Feature\Tenant;

use App\Http\Controllers\Api\Tenant\DashboardController;
use App\Models\Event;
use App\Models\EventType;
use App\Models\Package;
use App\Models\Photo;
use App\Models\Task;
use App\Models\Tenant;
use App\Models\TenantPackage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Tests\TestCase;

class DashboardSummaryTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_returns_dashboard_metrics_for_tenant(): void
    {
        app()->setLocale('de');

        $tenant = Tenant::factory()->create();

        $eventType = EventType::factory()->create();

        $eventWithTasks = Event::factory()->create([
            'tenant_id' => $tenant->id,
            'event_type_id' => $eventType->id,
            'status' => 'published',
            'is_active' => true,
            'date' => now()->addDays(3),
        ]);

        $eventWithoutTasks = Event::factory()->create([
            'tenant_id' => $tenant->id,
            'event_type_id' => $eventType->id,
            'status' => 'draft',
            'is_active' => false,
            'date' => now()->addDays(10),
        ]);

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

        $eventWithTasks->tasks()->attach($task->id);

        Photo::factory()->create([
            'event_id' => $eventWithTasks->id,
            'tenant_id' => $tenant->id,
            'created_at' => now()->subDays(2),
        ]);

        Photo::factory()->create([
            'event_id' => $eventWithTasks->id,
            'tenant_id' => $tenant->id,
            'created_at' => now()->subDays(8),
        ]);

        $package = Package::factory()
            ->reseller()
            ->create([
                'name' => 'Standard',
                'name_translations' => ['de' => 'Standard', 'en' => 'Standard'],
                'price' => 59,
            ]);

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

        $controller = new DashboardController;
        $request = Request::create('/api/v1/tenant/dashboard', 'GET');
        $request->attributes->set('decoded_token', ['tenant_id' => $tenant->id]);

        $response = $controller($request);

        $this->assertSame(200, $response->getStatusCode());

        $payload = $response->getData(true);

        $this->assertSame(1, Arr::get($payload, 'active_events'));
        $this->assertSame(1, Arr::get($payload, 'new_photos'));
        $this->assertSame(50, Arr::get($payload, 'task_progress'));
        $this->assertSame(2, Arr::get($payload, 'upcoming_events'));

        $activePackagePayload = Arr::get($payload, 'active_package');

        $this->assertIsArray($activePackagePayload);
        $this->assertSame('Standard', Arr::get($activePackagePayload, 'name'));
        $this->assertSame($activePackage->remaining_events, Arr::get($activePackagePayload, 'remaining_events'));

        $this->assertSame(
            $activePackage->expires_at->toIso8601String(),
            Arr::get($payload, 'active_package.expires_at')
        );
    }
}
