<?php

namespace Tests\Feature;

use App\Livewire\Projects\ProjectCompanies;
use App\Livewire\Projects\ProjectCompaniesTable;
use App\Livewire\Projects\ProjectUsers;
use App\Livewire\Projects\ProjectUsersTable;
use App\Models\Company;
use App\Models\Project;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Tests\TestCase;

class ProjectAssignmentsTest extends TestCase
{
    use RefreshDatabase;

    private User $admin;

    private Project $project;

    protected function setUp(): void
    {
        parent::setUp();
        foreach (['assign users', 'assign companies'] as $p) {
            Permission::findOrCreate($p);
        }
        $this->admin = User::factory()->create();
        $this->admin->givePermissionTo(['assign users', 'assign companies']);

        $this->project = Project::create([
            'reference' => 'ASG-1', 'name' => 'Proyecto Asig', 'address' => 'x',
            'lat' => 40.0, 'lng' => -3.0, 'start_date' => now()->toDateString(),
            'end_date_estimated' => now()->addMonth()->toDateString(),
            'status' => 'in_progress', 'created_by' => $this->admin->id,
        ]);
    }

    // ── Users ──────────────────────────────────────────────────────────────────────

    public function test_assign_user_and_table_lists_it(): void
    {
        $member = User::factory()->create(['name' => 'Zoe Member']);

        Livewire::actingAs($this->admin)
            ->test(ProjectUsers::class, ['project' => $this->project])
            ->set('selectedUserId', $member->id)
            ->set('selectedRole', 'supervisor')
            ->call('assignUser')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('project_user', [
            'project_id' => $this->project->id, 'user_id' => $member->id, 'role_in_project' => 'supervisor',
        ]);

        Livewire::actingAs($this->admin)
            ->test(ProjectUsersTable::class, ['projectId' => $this->project->id])
            ->assertOk()
            ->assertSee('Zoe Member');
    }

    public function test_users_table_change_role_and_remove(): void
    {
        $member = User::factory()->create();
        $this->project->users()->attach($member->id, ['role_in_project' => 'viewer']);

        Livewire::actingAs($this->admin)
            ->test(ProjectUsersTable::class, ['projectId' => $this->project->id])
            ->call('changeRole', $member->id, 'supervisor');
        $this->assertDatabaseHas('project_user', ['user_id' => $member->id, 'role_in_project' => 'supervisor']);

        Livewire::actingAs($this->admin)
            ->test(ProjectUsersTable::class, ['projectId' => $this->project->id])
            ->call('removeUser', $member->id);
        $this->assertDatabaseMissing('project_user', ['project_id' => $this->project->id, 'user_id' => $member->id]);
    }

    // ── Companies ────────────────────────────────────────────────────────────────────

    public function test_assign_company_requires_assign_companies_permission(): void
    {
        $weak = User::factory()->create();
        $weak->givePermissionTo('assign users'); // but NOT 'assign companies'
        $company = Company::create(['name' => 'ACME', 'estado' => 'activo', 'type' => 'constructor']);

        Livewire::actingAs($weak)
            ->test(ProjectCompanies::class, ['project' => $this->project])
            ->set('selectedCompanyId', $company->id)
            ->set('selectedRole', 'constructor')
            ->call('assignCompany')
            ->assertForbidden();
    }

    public function test_assign_company_and_table_lists_it(): void
    {
        $company = Company::create(['name' => 'Constructora Sur', 'estado' => 'activo', 'type' => 'constructor']);

        Livewire::actingAs($this->admin)
            ->test(ProjectCompanies::class, ['project' => $this->project])
            ->set('selectedCompanyId', $company->id)
            ->set('selectedRole', 'constructor')
            ->call('assignCompany')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('company_project', [
            'project_id' => $this->project->id, 'company_id' => $company->id, 'role_in_project' => 'constructor',
        ]);

        Livewire::actingAs($this->admin)
            ->test(ProjectCompaniesTable::class, ['projectId' => $this->project->id])
            ->assertOk()
            ->assertSee('Constructora Sur');
    }
}
