<?php

namespace Tests\Feature;

use App\Models\DamageFinding;
use App\Models\Inspection;
use App\Models\InspectionReport;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DashboardTest extends TestCase
{
    use RefreshDatabase;

    public function test_dashboard_displays_dynamic_inspection_data(): void
    {
        $user = User::factory()->create([
            'name' => 'Demo Inspector',
            'role' => 'Inspector',
        ]);

        $inspection = Inspection::query()->create([
            'user_id' => $user->id,
            'property_name' => 'Dynamic Test House',
            'property_address' => '123 Test Street',
            'status' => 'completed',
            'ai_confidence' => 97.5,
            'estimated_cost' => 1500000,
            'scheduled_at' => now(),
            'completed_at' => now(),
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $inspection->id,
            'category' => 'Wall crack',
            'severity' => 'medium',
            'status' => 'review',
            'confidence' => 91.4,
            'estimated_repair_cost' => 650000,
        ]);

        InspectionReport::query()->create([
            'inspection_id' => $inspection->id,
            'report_number' => 'RPT-TEST',
            'status' => 'ready',
        ]);

        $response = $this->actingAs($user)->get('/dashboard');

        $response->assertOk();
        $response->assertSee('Dynamic Test House');
        $response->assertSee('1 findings need review');
        $response->assertSee('97.5%');
    }

    public function test_dashboard_only_displays_current_user_data(): void
    {
        $currentUser = User::factory()->create(['role' => 'Inspector']);
        $otherUser = User::factory()->create(['role' => 'Inspector']);

        $ownInspection = Inspection::query()->create([
            'user_id' => $currentUser->id,
            'property_name' => 'Own Dashboard House',
            'property_address' => 'Own Street',
            'status' => 'completed',
            'ai_confidence' => 90,
            'estimated_cost' => 500000,
            'scheduled_at' => now(),
            'completed_at' => now(),
        ]);
        $otherInspection = Inspection::query()->create([
            'user_id' => $otherUser->id,
            'property_name' => 'Other User House',
            'property_address' => 'Other Street',
            'status' => 'completed',
            'ai_confidence' => 99,
            'estimated_cost' => 9999999,
            'scheduled_at' => now()->addMinute(),
            'completed_at' => now(),
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $ownInspection->id,
            'category' => 'Own leak',
            'severity' => 'medium',
            'status' => 'review',
            'confidence' => 90,
            'estimated_repair_cost' => 500000,
        ]);
        DamageFinding::query()->create([
            'inspection_id' => $otherInspection->id,
            'category' => 'Other leak',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 99,
            'estimated_repair_cost' => 9999999,
        ]);
        InspectionReport::query()->create([
            'inspection_id' => $ownInspection->id,
            'report_number' => 'RPT-OWN',
            'status' => 'ready',
        ]);
        InspectionReport::query()->create([
            'inspection_id' => $otherInspection->id,
            'report_number' => 'RPT-OTHER',
            'status' => 'ready',
        ]);

        $response = $this->actingAs($currentUser)->get('/dashboard');

        $response->assertOk();
        $response->assertSee('Own Dashboard House');
        $response->assertDontSee('Other User House');
        $response->assertSee('1 findings need review');
        $response->assertDontSee('Rp 9.999.999');
    }
}
