<?php

namespace App\Http\Controllers;
use App\Models\Group;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;

class SelectListController extends Controller
{
    public function getRoles()
    {
        $roles = Role::select('id', 'name')->get();

        $formattedRoles = [];

        foreach ($roles as $role) {
            $formattedRoles[] = [
                'id' => $role->id,
                'name' => $role->name,
            ];
        }

        return response()->json(['roles' => $formattedRoles]);
    }

    public function getUsers()
    {
        $users = User::select('id', 'username', 'email')->get();

        $formattedUsers = [];

        foreach ($users as $user) {
            $formattedUsers[] = [
                'id' => $user->id,
                'name' => $user->username,
                'email' => $user->email,
            ];
        }

        return response()->json(['users' => $formattedUsers]);
    }

    public function getGroups()
    {
        $users = Group::select('id', 'name')->get();
        $formattedGroups = [];

        foreach ($users as $user) {
            $formattedGroups[] = [
                'id' => $user->id,
                'name' => $user->name,
            ];
        }

        return response()->json(['groups' => $formattedGroups]);
    }
}
