<?php

namespace Tests\Browser;

use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class GatePassFlowTest extends DuskTestCase
{
    public function test_gatepass_create_and_status_flow(): void
    {
        $this->browse(function (Browser $browser) {
            // Login as owner (server-side auth to avoid brittle selectors)
            $user = \App\Models\User::where('email', 'owner@erp.local')->first();
            if ($user) { $user->forceFill(['email_verified_at'=>now(),'is_active'=>1])->save(); }
            $browser->loginAs($user)
                ->visit('/');

            // Navigate via index page button to avoid route naming confusion
            // Ensure there is at least one Purchase Order to select in IGP form
            $po = \App\Models\PurchaseOrder::first();
            if (!$po) {
                // Create minimal PR → PO chain
                $cat = \App\Models\ItemCategory::first() ?: \App\Models\ItemCategory::create(['name' => 'DuskCat '.uniqid()]);
                $uom = \App\Models\Uom::first() ?: \App\Models\Uom::create(['code' => 'PCS', 'name' => 'Pieces']);
                $item = \App\Models\Item::first() ?: \App\Models\Item::create([
                    'name' => 'Dusk Item IGP', 'sku' => 'RM-'.mt_rand(1000,9999), 'type' => 'raw_material',
                    'item_category_id' => $cat->id, 'uom_id' => $uom->id, 'unit_of_measure' => $uom->code, 'status' => 'active'
                ]);
                $vendor = \App\Models\Contact::firstOrCreate(['email'=>'vendor.dusk@local'], ['name'=>'Vendor Dusk','type'=>'vendor','is_active'=>1]);
                $pr = \App\Models\PurchaseRequest::create(['pr_number'=>'PR-DUSK-'.mt_rand(100,999), 'status'=>'approved']);
                \App\Models\PurchaseRequestItem::create(['purchase_request_id'=>$pr->id,'item_id'=>$item->id,'quantity'=>1,'unit_code'=>$uom->code]);
                $po = \App\Models\PurchaseOrder::create(['po_number'=>'PO-DUSK-'.mt_rand(100,999),'purchase_request_id'=>$pr->id,'vendor_id'=>$vendor->id,'order_date'=>now()]);
                \App\Models\PurchaseOrderItem::create(['purchase_order_id'=>$po->id,'item_id'=>$item->id,'quantity'=>1,'unit_price'=>100,'unit_code'=>$uom->code]);
            }

            $browser->visit('/gatepasses/create')
                ->waitFor('input[name="gate_pass_number"]', 10);

            // Minimal presence of form controls (smoke)
            $browser->assertPresent('select[name="type"]')
                ->assertPresent('input[name="vehicle_no"]')
                ->assertPresent('#po_search')
                ->assertPresent('input[name="gate_pass_number"]');

            // Fill minimal fields and submit
            $browser->type('vehicle_no', 'KTZ-'.mt_rand(100,999))
                ->select('type', 'inbound');
            // Select PO via select2 script
            $browser->script(["$('#po_search').append(new Option('{$po->po_number}','{$po->id}',true,true)).trigger('change');"]);
            $browser->press('Create')
                ->waitForLocation('/gatepasses', 10);
        });
    }
}


