<?php

namespace Tests\Feature;

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

class InspectionTest extends TestCase
{
    use RefreshDatabase;

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

        $inspection = Inspection::query()->create([
            'user_id' => $user->id,
            'property_name' => 'Filtered Inspection House',
            'property_address' => '9 Dynamic Lane',
            'status' => 'review',
            'ai_confidence' => 88.5,
            'estimated_cost' => 2400000,
            'scheduled_at' => now(),
            'source_image_paths' => [
                [
                    'name' => 'front elevation.jpg',
                    'url' => '/storage/inspections/source/front-elevation.jpg',
                    'path' => 'inspections/source/front-elevation.jpg',
                ],
            ],
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $inspection->id,
            'category' => 'Roof leak',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 93.2,
            'estimated_repair_cost' => 1200000,
        ]);

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

        $response = $this->actingAs($user)->get('/inspections?status=review&severity=high&search=Filtered');

        $response->assertOk();
        $response->assertSee('Filtered Inspection House');
        $response->assertSee('Roof leak');
        $response->assertSee('88.5%');
        $response->assertSee('/storage/inspections/source/front-elevation.jpg');
        $response->assertSee('front elevation.jpg');
        $response->assertSee('id="successPanel" class="hidden', false);
        $response->assertSee('id="scanOverlay" class="hidden', false);
    }

    public function test_inspection_page_filters_records_and_findings_by_workspace(): void
    {
        $user = User::factory()->create(['role' => 'Inspector']);
        $workspaceA = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Rumah Utama',
            'default_currency' => 'IDR',
        ]);
        $workspaceB = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Rumah Lain',
            'default_currency' => 'IDR',
        ]);
        $collectionA = InspectionCollection::query()->create([
            'user_id' => $user->id,
            'inspection_workspace_id' => $workspaceA->id,
            'name' => 'Kamar Utama',
            'collection_type' => 'room',
            'property_name' => 'Rumah Utama',
            'room_name' => 'Kamar Utama',
        ]);
        $collectionB = InspectionCollection::query()->create([
            'user_id' => $user->id,
            'inspection_workspace_id' => $workspaceB->id,
            'name' => 'Dapur Rumah Lain',
            'collection_type' => 'room',
            'property_name' => 'Rumah Lain',
            'room_name' => 'Dapur',
        ]);

        $inspectionA = Inspection::query()->create([
            'user_id' => $user->id,
            'inspection_collection_id' => $collectionA->id,
            'property_name' => 'Plafon Rumah Utama',
            'property_address' => 'Jl. Utama',
            'status' => 'review',
            'ai_confidence' => 88,
            'estimated_cost' => 14000000,
            'scheduled_at' => now(),
        ]);
        $inspectionB = Inspection::query()->create([
            'user_id' => $user->id,
            'inspection_collection_id' => $collectionB->id,
            'property_name' => 'Dinding Rumah Lain',
            'property_address' => 'Jl. Lain',
            'status' => 'review',
            'ai_confidence' => 92,
            'estimated_cost' => 2500000,
            'scheduled_at' => now(),
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $inspectionA->id,
            'category' => 'Ceiling leak',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 90,
            'estimated_repair_cost' => 5000000,
        ]);
        DamageFinding::query()->create([
            'inspection_id' => $inspectionB->id,
            'category' => 'Other workspace crack',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 91,
            'estimated_repair_cost' => 2500000,
        ]);

        $response = $this->actingAs($user)->get(route('inspections.index', [
            'workspace_id' => $workspaceA->id,
            'collection_id' => $collectionB->id,
        ]));

        $response->assertOk();
        $response->assertSee('Plafon Rumah Utama');
        $response->assertSee('Ceiling leak');
        $response->assertDontSee('Dinding Rumah Lain');
        $response->assertDontSee('Other workspace crack');
    }
}
