<?php

namespace Tests\Feature;

use App\Enums\UserRole;
use App\Models\Business;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AuthenticationFoundationTest extends TestCase
{
    use RefreshDatabase;

    public function test_public_routes_are_available_without_authentication(): void
    {
        $this->get('/')->assertOk();
        $this->get('/login')->assertOk();
        $this->get('/register')->assertOk();
        $this->get('/forgot-password')->assertOk();
        $this->get('/reset-password')->assertOk();

        Business::factory()->create([
            'name' => 'Glow Beauty Salon',
            'slug' => 'glow-beauty-salon',
            'status' => 'active',
        ]);

        $this->get('/book/glow-beauty-salon')->assertOk();
    }

    public function test_dashboard_routes_require_authentication(): void
    {
        $this->get('/dashboard')->assertRedirect('/login');
        $this->get('/appointments')->assertRedirect('/login');
        $this->get('/super-admin/dashboard')->assertRedirect('/login');
    }

    public function test_login_redirects_business_owner_to_dashboard(): void
    {
        User::factory()->create([
            'email' => 'owner@booklio.test',
            'password' => 'password',
            'role' => UserRole::BusinessOwner->value,
        ]);

        $this->post('/login', [
            'email' => 'owner@booklio.test',
            'password' => 'password',
        ])->assertRedirect('/dashboard');
    }

    public function test_login_redirects_staff_to_dashboard(): void
    {
        User::factory()->create([
            'email' => 'staff@booklio.test',
            'password' => 'password',
            'role' => UserRole::Staff->value,
        ]);

        $this->post('/login', [
            'email' => 'staff@booklio.test',
            'password' => 'password',
        ])->assertRedirect('/dashboard');
    }

    public function test_login_redirects_super_admin_to_platform_dashboard(): void
    {
        User::factory()->create([
            'email' => 'admin@booklio.test',
            'password' => 'password',
            'role' => UserRole::SuperAdmin->value,
        ]);

        $this->post('/login', [
            'email' => 'admin@booklio.test',
            'password' => 'password',
        ])->assertRedirect('/super-admin/dashboard');
    }
}
