<?php

namespace Tests\Browser\PMCore;

use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class ProjectBrowserTest extends DuskTestCase
{
    protected ?User $adminUser = null;

    protected function setUp(): void
    {
        parent::setUp();
        $this->adminUser = User::where('email', 'admin@demo.com')->first();
    }

    public function test_projects_index_page_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/projects')
                ->assertPathIs('/projects')
                ->assertSee('Project');
        });
    }

    public function test_projects_datatable_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/projects')
                ->waitFor('.datatables-projects', 10)
                ->assertPresent('table');
        });
    }

    public function test_create_project_page_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/projects/create')
                ->assertPathIs('/projects/create')
                ->assertSee('Create')
                ->assertPresent('form');
        });
    }

    public function test_can_navigate_to_create_project(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/projects')
                ->waitFor('a.btn-primary[href*="create"]', 5)
                ->click('a.btn-primary[href*="create"]')
                ->pause(1000)
                ->assertPathIs('/projects/create')
                ->assertPresent('form');
        });
    }

    public function test_can_view_project_details(): void
    {
        // Get a project from database
        $project = \Modules\PMCore\App\Models\Project::first();

        if (! $project) {
            $this->markTestSkipped('No projects available in database');
        }

        $this->browse(function (Browser $browser) use ($project) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit("/projects/{$project->id}")
                ->assertPathIs("/projects/{$project->id}")
                ->assertSee($project->name)
                ->assertPresent('.card');
        });
    }

    public function test_project_search_works(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/projects')
                ->waitFor('.datatables-projects', 10)
                ->waitFor('.dataTables_filter input', 5)
                ->type('.dataTables_filter input', 'test')
                ->pause(2000);
        });
    }

    public function test_guest_cannot_access_projects(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->visit('/projects')
                ->assertPathIs('/auth/login');
        });
    }
}
