<?php

namespace Tests\Feature;

use App\Livewire\Projects\FeatureTable;
use App\Livewire\Projects\InspectionTable;
use App\Models\Feature;
use App\Models\Inspection;
use App\Models\Layer;
use App\Models\Phase;
use App\Models\Project;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class MapTablesTest extends TestCase
{
    use RefreshDatabase;

    private User $user;

    private Project $project;

    private Feature $feature;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user = User::factory()->create();
        $this->project = Project::create([
            'reference' => 'MAP-1', 'name' => 'Proyecto Mapa', 'address' => 'x',
            'lat' => 40.0, 'lng' => -3.0, 'start_date' => now()->toDateString(),
            'end_date_estimated' => now()->addMonth()->toDateString(),
            'status' => 'in_progress', 'created_by' => $this->user->id,
        ]);
        $this->project->users()->attach($this->user->id, ['role_in_project' => 'supervisor']);

        $phase = Phase::create(['project_id' => $this->project->id, 'name' => 'Fase 1', 'order' => 1, 'color' => '#3b82f6', 'progress_percent' => 0]);
        $layer = Layer::create(['project_id' => $this->project->id, 'phase_id' => $phase->id, 'name' => 'Capa A', 'color' => '#10b981', 'uploaded_by' => $this->user->id]);
        $this->feature = Feature::create([
            'layer_id' => $layer->id, 'name' => 'Pilar 12',
            'geometry' => ['type' => 'Point', 'coordinates' => [-3.0, 40.0]],
            'progress' => 50, 'status' => 'in_progress',
        ]);
    }

    public function test_feature_table_lists_project_features(): void
    {
        Livewire::actingAs($this->user)
            ->test(FeatureTable::class, ['projectId' => $this->project->id])
            ->assertOk()
            ->assertSee('Pilar 12')
            ->assertSee('Capa A');
    }

    public function test_feature_table_forbidden_for_non_member(): void
    {
        $outsider = User::factory()->create();
        Livewire::actingAs($outsider)
            ->test(FeatureTable::class, ['projectId' => $this->project->id])
            ->assertForbidden();
    }

    public function test_feature_table_layer_filter(): void
    {
        // Segunda capa con otro elemento en la misma fase.
        $phaseId = $this->feature->layer->phase_id;
        $layerB = Layer::create(['project_id' => $this->project->id, 'phase_id' => $phaseId, 'name' => 'Capa B', 'color' => '#222', 'uploaded_by' => $this->user->id]);
        Feature::create([
            'layer_id' => $layerB->id, 'name' => 'Viga 7',
            'geometry' => ['type' => 'Point', 'coordinates' => [-3.0, 40.0]],
            'progress' => 0, 'status' => 'planned',
        ]);

        Livewire::actingAs($this->user)
            ->test(FeatureTable::class, ['projectId' => $this->project->id])
            ->assertSee('Pilar 12')
            ->assertSee('Viga 7')
            ->set('filterComponents.layer', (string) $this->feature->layer_id)
            ->assertSee('Pilar 12')
            ->assertDontSee('Viga 7');
    }

    public function test_inspection_table_lists_project_inspections(): void
    {
        Inspection::create([
            'project_id' => $this->project->id,
            'layer_id' => $this->feature->layer_id,
            'feature_id' => $this->feature->id,
            'user_id' => $this->user->id,
            'data' => ['ok' => true],
            'status' => 'completed',
            'result' => 'pass',
        ]);

        Livewire::actingAs($this->user)
            ->test(InspectionTable::class, ['projectId' => $this->project->id])
            ->assertOk()
            ->assertSee('Pilar 12')
            ->assertSee('pass');
    }
}
