<?php

namespace Tests\Unit\Services\Roles;

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

class RoleAssignmentServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_assign_roles_creates_user_roles_and_staff(): void
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'assign@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        $roleId = DB::table('roles')->insertGetId([
            'name' => 'admin',
            'slug' => 'admin',
            'description' => 'Admin',
            'dashboard_route' => 'administrator/administratordashboard',
            'priority' => 1,
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new RoleAssignmentService(new RoleStaffService());
        $result = $service->assignRoles($userId, [$roleId], $userId);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('user_roles', [
            'user_id' => $userId,
            'role_id' => $roleId,
        ]);
        $this->assertDatabaseHas('staff', [
            'user_id' => $userId,
        ]);
    }
}
