<?php

namespace Tests\Browser\WMSInventoryCore;

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

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

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

    public function test_warehouses_index_page_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/inventory/warehouses')
                ->assertPathIs('/inventory/warehouses')
                ->assertSee('Warehouse');
        });
    }

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

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

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

    public function test_can_view_warehouse_details(): void
    {
        // Get a warehouse from database
        $warehouse = \Modules\WMSInventoryCore\App\Models\Warehouse::first();

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

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

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

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