<?php

namespace Tests\Feature;

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

class CostEstimationTest extends TestCase
{
    use RefreshDatabase;

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

        $inspection = Inspection::query()->create([
            'user_id' => $user->id,
            'property_name' => 'Cost View House',
            'property_address' => '8 Estimate Street',
            'status' => 'review',
            'ai_confidence' => 87.8,
            'estimated_cost' => 8000000,
            'scheduled_at' => now(),
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $inspection->id,
            'category' => 'Foundation crack',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 94.7,
            'estimated_repair_cost' => 3500000,
        ]);

        $response = $this->actingAs($user)->get('/cost-estimation?severity=high&search=Foundation');

        $response->assertOk();
        $response->assertSee('Foundation crack');
        $response->assertSee('Cost View House');
        $response->assertSee('Rp 3.500.000');
    }

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

        $inspectionA = Inspection::query()->create([
            'user_id' => $user->id,
            'inspection_collection_id' => $collectionA->id,
            'property_name' => 'Rumah A / Kamar A',
            'property_address' => 'A Street',
            'status' => 'review',
            'ai_confidence' => 90,
            'estimated_cost' => 1000000,
            'scheduled_at' => now(),
        ]);
        $inspectionB = Inspection::query()->create([
            'user_id' => $user->id,
            'inspection_collection_id' => $collectionB->id,
            'property_name' => 'Rumah B / Kamar B',
            'property_address' => 'B Street',
            'status' => 'review',
            'ai_confidence' => 90,
            'estimated_cost' => 9000000,
            'scheduled_at' => now(),
        ]);

        DamageFinding::query()->create([
            'inspection_id' => $inspectionA->id,
            'category' => 'Workspace A leak',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 92,
            'estimated_repair_cost' => 1000000,
        ]);
        DamageFinding::query()->create([
            'inspection_id' => $inspectionB->id,
            'category' => 'Workspace B crack',
            'severity' => 'high',
            'status' => 'review',
            'confidence' => 92,
            'estimated_repair_cost' => 9000000,
        ]);

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

        $response->assertOk();
        $response->assertSee('Workspace A leak');
        $response->assertDontSee('Workspace B crack');
        $response->assertSee('Rp 1.000.000');
    }
}
