<?php

namespace Tests\Browser\SystemCore;

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

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

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

    public function test_customers_index_page_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/systemcore/customers')
                ->assertPathIs('/systemcore/customers')
                ->assertSee('Customer');
        });
    }

    public function test_customers_datatable_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/systemcore/customers')
                ->waitFor('#customersTable', 10)
                ->assertPresent('table');
        });
    }

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

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

    public function test_can_view_customer_details(): void
    {
        // Get a customer from database
        $customer = \Modules\SystemCore\App\Models\CoreCustomer::first();

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

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

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

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