<?php

namespace App\Services\Administrator;

use Illuminate\Support\Facades\DB;

class AdminNotificationUserService
{
    public function excludedRoles(): array
    {
        return [
            'parent',
            'student',
            'guest',
            'teacher',
            'assistant teacher',
            'teacher assistant',
            'teacher_assistant',
            'assistant_teacher',
            'ta',
            'authorized_user',
        ];
    }

    public function fetchAdminNotificationUsers(): array
    {
        $excluded = $this->excludedRoles();

        return DB::table('users as u')
            ->select('u.id', 'u.firstname', 'u.lastname', 'u.email')
            ->join('user_roles as ur', 'ur.user_id', '=', 'u.id')
            ->join('roles as r', 'r.id', '=', 'ur.role_id')
            ->whereNotNull('r.name')
            ->whereNull('ur.deleted_at')
            ->whereNotIn(DB::raw('LOWER(r.name)'), array_map('strtolower', $excluded))
            ->groupBy('u.id', 'u.firstname', 'u.lastname', 'u.email')
            ->orderBy('u.lastname')
            ->orderBy('u.firstname')
            ->get()
            ->map(fn($r) => (array) $r)
            ->all();
    }
}