<?php

namespace Tests\Feature;

use App\Models\Notification;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PwaHomeTest extends TestCase
{
    use RefreshDatabase;

    public function test_pwa_home_requires_authentication(): void
    {
        $this->getJson('/api/pwa/home')->assertUnauthorized();
    }

    public function test_authenticated_user_gets_personal_pwa_home_summary(): void
    {
        $user = User::factory()->create(['status' => 'active', 'name' => 'کاربر تست']);
        $project = Project::create([
            'title' => 'پروژه موبایل',
            'project_manager_id' => $user->id,
            'status' => 'active',
            'priority' => 'medium',
            'created_by' => $user->id,
        ]);

        Task::create([
            'title' => 'تسک امروز',
            'project_id' => $project->id,
            'assignee_id' => $user->id,
            'reporter_id' => $user->id,
            'created_by' => $user->id,
            'priority' => 'high',
            'status' => 'in_progress',
            'due_date' => now()->toDateString(),
        ]);

        Notification::create([
            'user_id' => $user->id,
            'type' => 'task_assigned',
            'title' => 'تسک جدید',
            'body' => 'یک تسک به شما تخصیص داده شد',
            'is_read' => false,
        ]);

        $token = $user->createToken('api-token')->plainTextToken;

        $this->withToken($token)
            ->getJson('/api/pwa/home')
            ->assertOk()
            ->assertJsonPath('data.user.name', 'کاربر تست')
            ->assertJsonPath('data.stats.today_tasks', 1)
            ->assertJsonPath('data.stats.unread_notifications', 1)
            ->assertJsonPath('data.tasks.0.title', 'تسک امروز')
            ->assertJsonPath('data.notifications.0.title', 'تسک جدید');
    }
}
