<?php

namespace Tests\Browser\CRMCore;

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

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

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

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

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

    public function test_create_lead_form_opens(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/leads')
                ->waitFor('#add-new-lead-btn', 5)
                ->click('#add-new-lead-btn')
                ->waitFor('#offcanvasLeadForm.show', 3)
                ->assertPresent('#leadForm');
        });
    }

    public function test_lead_kanban_view_loads(): void
    {
        $this->browse(function (Browser $browser) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/leads')
                ->waitFor('#btn-kanban-view', 5)
                ->click('#btn-kanban-view')
                ->pause(1000)
                ->waitFor('#kanban-view-container:not(.d-none)', 5)
                ->assertPresent('.kanban-board');
        });
    }

    public function test_can_create_lead(): void
    {
        $leadTitle = 'Test Lead ' . time();

        $this->browse(function (Browser $browser) use ($leadTitle) {
            $browser->driver->manage()->deleteAllCookies();
            $browser->loginAs($this->adminUser)
                ->visit('/leads')
                ->waitFor('#add-new-lead-btn', 5)
                ->click('#add-new-lead-btn')
                ->waitFor('#offcanvasLeadForm.show', 3)
                ->type('#title', $leadTitle)
                ->type('#contact_name', 'John Test')
                ->type('#contact_email', 'john.test.' . time() . '@example.com')
                ->pause(500);

            // Submit the form
            $browser->click('#saveLeadBtn')
                ->pause(2000);

            // Verify lead appears in the list or success message
            $browser->visit('/leads')
                ->waitFor('#leadsTable', 10);
        });
    }

    public function test_can_view_lead_details(): void
    {
        // Get a lead from database
        $lead = \Modules\CRMCore\App\Models\Lead::first();

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

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

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

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