<?php

use App\Models\User;

use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
use function Pest\Laravel\post;

test('login page is accessible', function () {
    get(route('login'))
        ->assertStatus(200)
        ->assertSee('RESTAURANT ADMIN');
});

test('super admin can login with correct credentials', function () {
    $user = User::factory()->create([
        'email' => 'admin@test.com',
        'password' => bcrypt('password'),
        'role' => 'super_admin',
        'active' => 1,
    ]);

    post(url('super-admin/login'), [
        'email' => 'admin@test.com',
        'password' => 'password',
    ])
        ->assertRedirect(route('super-admin.dashboard'));

    expect(auth()->user()->id)->toBe($user->id);
});

test('non super admin cannot login to admin panel', function () {
    $user = User::factory()->create([
        'email' => 'user@test.com',
        'password' => bcrypt('password'),
        'role' => 'client',
        'active' => 1,
    ]);

    post(url('super-admin/login'), [
        'email' => 'user@test.com',
        'password' => 'password',
    ])
        ->assertSessionHasErrors('email');

    expect(auth()->check())->toBeFalse();
});

test('inactive super admin cannot login', function () {
    $user = User::factory()->create([
        'email' => 'inactive@test.com',
        'password' => bcrypt('password'),
        'role' => 'super_admin',
        'active' => 0,
    ]);

    post(url('super-admin/login'), [
        'email' => 'inactive@test.com',
        'password' => 'password',
    ])
        ->assertSessionHasErrors('email');

    expect(auth()->check())->toBeFalse();
});

test('authenticated super admin can access dashboard', function () {
    $user = User::factory()->create([
        'role' => 'super_admin',
        'active' => 1,
    ]);

    actingAs($user)
        ->get(route('super-admin.dashboard'))
        ->assertStatus(200)
        ->assertSee('Platform Overview');
});

test('unauthenticated user cannot access dashboard', function () {
    get(route('super-admin.dashboard'))
        ->assertRedirect(route('login'));
});

test('super admin can logout', function () {
    $user = User::factory()->create([
        'role' => 'super_admin',
        'active' => 1,
    ]);

    actingAs($user)
        ->post(route('super-admin.logout'))
        ->assertRedirect(route('login'));

    expect(auth()->check())->toBeFalse();
});
