<?php

namespace Tests\Feature;

use App\Models\Branch;
use App\Models\Business;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DashboardFilterControlsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
    }

    public function test_dashboard_branch_filter_defaults_to_all_branches_and_shows_reset(): void
    {
        $admin = User::where('username', 'admin')->firstOrFail();
        $branch = $this->branch('Dashboard Filter Default');

        $this->actingAs($admin)
            ->get(route('dashboard'))
            ->assertOk()
            ->assertSee('data-dashboard-branch-picker', false)
            ->assertSee('All branches')
            ->assertSee('Reset')
            ->assertSee('Apply Dashboard Filters')
            ->assertDontSee('name="branch_ids[]" value="'.$branch->id.'" checked', false);
    }

    public function test_dashboard_branch_filter_keeps_an_explicit_branch_selected(): void
    {
        $admin = User::where('username', 'admin')->firstOrFail();
        $branch = $this->branch('Dashboard Filter Selected');

        $this->actingAs($admin)
            ->get(route('dashboard', ['branch_ids' => [$branch->id]]))
            ->assertOk()
            ->assertSee('name="branch_ids[]" value="'.$branch->id.'" checked', false);
    }

    private function branch(string $name): Branch
    {
        return Branch::create([
            'business_id' => Business::firstOrFail()->id,
            'name' => $name,
            'status' => 'active',
        ]);
    }
}
