<?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 ReportTest extends TestCase
{
    use RefreshDatabase;

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

        $inspection = Inspection::query()->create([
            'user_id' => $user->id,
            'property_name' => 'Report View House',
            'property_address' => '5 Report Road',
            'status' => 'completed',
            'ai_confidence' => 98.2,
            'estimated_cost' => 4500000,
            'scheduled_at' => now(),
            'completed_at' => now(),
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $inspection->id,
            'category' => 'Ceiling stain',
            'severity' => 'medium',
            'status' => 'confirmed',
            'confidence' => 90.1,
            'estimated_repair_cost' => 900000,
        ]);

        InspectionReport::query()->create([
            'inspection_id' => $inspection->id,
            'report_number' => 'RPT-DYNAMIC',
            'status' => 'ready',
            'summary' => 'Dynamic report summary.',
            'published_at' => now(),
        ]);

        $response = $this->actingAs($user)->get('/reports?status=ready&search=RPT-DYNAMIC');

        $response->assertOk();
        $response->assertSee('RPT-DYNAMIC');
        $response->assertSee('Report View House');
        $response->assertSee('Dynamic report summary');
    }

    public function test_reports_page_filters_by_workspace(): void
    {
        $user = User::factory()->create(['role' => 'Inspector']);
        $workspaceA = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Report Workspace A',
            'default_currency' => 'IDR',
        ]);
        $workspaceB = InspectionWorkspace::query()->create([
            'user_id' => $user->id,
            'name' => 'Report Workspace B',
            'default_currency' => 'IDR',
        ]);
        $collectionA = InspectionCollection::query()->create([
            'user_id' => $user->id,
            'inspection_workspace_id' => $workspaceA->id,
            'name' => 'Collection A',
            'collection_type' => 'room',
            'property_name' => 'House A',
            'room_name' => 'Room A',
        ]);
        $collectionB = InspectionCollection::query()->create([
            'user_id' => $user->id,
            'inspection_workspace_id' => $workspaceB->id,
            'name' => 'Collection B',
            'collection_type' => 'room',
            'property_name' => 'House B',
            'room_name' => 'Room B',
        ]);
        $inspectionA = Inspection::query()->create([
            'user_id' => $user->id,
            'inspection_collection_id' => $collectionA->id,
            'property_name' => 'Report House A',
            'property_address' => 'A Street',
            'status' => 'completed',
            'ai_confidence' => 91,
            'estimated_cost' => 1000000,
            'scheduled_at' => now(),
            'completed_at' => now(),
        ]);
        $inspectionB = Inspection::query()->create([
            'user_id' => $user->id,
            'inspection_collection_id' => $collectionB->id,
            'property_name' => 'Report House B',
            'property_address' => 'B Street',
            'status' => 'completed',
            'ai_confidence' => 91,
            'estimated_cost' => 1000000,
            'scheduled_at' => now(),
            'completed_at' => now(),
        ]);

        InspectionReport::query()->create([
            'inspection_id' => $inspectionA->id,
            'report_number' => 'RPT-WORKSPACE-A',
            'status' => 'ready',
            'summary' => 'Workspace A summary.',
            'published_at' => now(),
        ]);
        InspectionReport::query()->create([
            'inspection_id' => $inspectionB->id,
            'report_number' => 'RPT-WORKSPACE-B',
            'status' => 'ready',
            'summary' => 'Workspace B summary.',
            'published_at' => now(),
        ]);

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

        $response->assertOk();
        $response->assertSee('RPT-WORKSPACE-A');
        $response->assertDontSee('RPT-WORKSPACE-B');
        $response->assertSee('Report Workspace A');
    }

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

        $inspection = Inspection::query()->create([
            'user_id' => $owner->id,
            'property_name' => 'Private Report House',
            'property_address' => 'Private Street',
            'status' => 'completed',
            'ai_confidence' => 94,
            'estimated_cost' => 1200000,
            'scheduled_at' => now(),
            'completed_at' => now(),
        ]);

        $report = InspectionReport::query()->create([
            'inspection_id' => $inspection->id,
            'report_number' => 'RPT-PRIVATE',
            'status' => 'ready',
            'summary' => 'Private report summary.',
            'published_at' => now(),
        ]);

        $this->actingAs($otherUser)
            ->get(route('reports.show', $report))
            ->assertForbidden();
    }
}
