<?php

namespace Tests\Unit\Services\Frontend;

use App\Models\User;
use App\Services\Frontend\LandingPageRoleService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class LandingPageRoleServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_resolve_role_returns_guest_when_missing(): void
    {
        $service = app(LandingPageRoleService::class);
        $this->assertSame('guest', $service->resolveRole(null));
    }

    public function test_resolve_role_returns_first_role(): void
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'teacher',
            'priority' => 1,
            'is_active' => 1,
        ]);

        $user = User::query()->create([
            'firstname' => 'Teach',
            'lastname' => 'Er',
            'email' => 'teacher@example.com',
            'cellphone' => '5555555555',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'status' => 'Active',
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $roleId,
        ]);

        $service = app(LandingPageRoleService::class);
        $this->assertSame('teacher', $service->resolveRole($user));
    }
}
