<?php

use App\Models\Branch;
use App\Models\User;

use function Pest\Laravel\actingAs;

beforeEach(function () {
    $this->admin = User::factory()->create(['role' => 'super_admin', 'active' => 1]);
});

test('super admin can list clients', function () {
    User::factory()->count(3)->create(['role' => 'client']);

    actingAs($this->admin)
        ->get(route('clients.index'))
        ->assertStatus(200)
        ->assertSee('Registered Clients');
});

test('super admin can create a client', function () {
    actingAs($this->admin)
        ->post(route('clients.store'), [
            'name' => 'New Client',
            'email' => 'client@test.com',
            'password' => 'password123',
            'active' => 1,
        ])
        ->assertRedirect(route('clients.index'));

    expect(User::where('email', 'client@test.com')->exists())->toBeTrue();
});

test('super admin can list branches', function () {
    $client = User::factory()->create(['role' => 'client']);
    Branch::create([
        'user_id' => $client->id,
        'name' => 'Main Branch',
        'slug' => 'main-branch',
        'active' => 1,
    ]);

    actingAs($this->admin)
        ->get(route('branches.index'))
        ->assertStatus(200)
        ->assertSee('Main Branch');
});

test('super admin can create a branch for a client', function () {
    $client = User::factory()->create(['role' => 'client']);

    actingAs($this->admin)
        ->post(route('branches.store'), [
            'user_id' => $client->id,
            'name' => 'North Branch',
            'slug' => 'north-branch',
            'address' => '123 Street',
            'phone' => '123456789',
            'active' => 1,
        ])
        ->assertRedirect(route('branches.index'));

    expect(Branch::where('slug', 'north-branch')->exists())->toBeTrue();
});
