<?php

namespace Tests\Unit\Services\Roles;

use App\Services\Roles\RoleQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class RoleQueryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_roles_returns_paginator(): void
    {
        DB::table('roles')->insert([
            'name' => 'admin',
            'slug' => 'admin',
            'description' => 'Admin',
            'dashboard_route' => 'administrator/administratordashboard',
            'priority' => 1,
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new RoleQueryService();
        $page = $service->listRoles([]);

        $this->assertCount(1, $page->items());
    }

    public function test_list_users_with_roles_returns_roles(): void
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'role.query@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(),
        ]);

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

        $service = new RoleQueryService();
        $page = $service->listUsersWithRoles([]);

        $this->assertCount(1, $page->items());
        $this->assertStringContainsString('admin', $page->items()[0]->roles);
    }
}
