<?php

if (!function_exists('demandStatus')) {
    function demandStatus(string $status): string
    {
        $final = '';
        switch ($status) {
            case 'pending':
                $final = 'En attente';
                break;
            case 'confirmed':
                $final = 'Confirmé';
                break;
            case 'rejected':
                $final = 'Rejeté';
                break;
            case 'canceled':
                $final = 'Annulé';
                break;
            case 'rescheduled':
                $final = 'Reprogrammé';
                break;
            case 'processing':
                $final = 'En cours de traitement';
                break;
            case 'occurred':
                $final = 'Auditée';
            default:
                $final = '';
                break;
        }
        return $final;
    }
}


function userRole(string $status): string
{
    $final = '';
    switch ($status) {
        case 'vr-aa':
            $final = 'Vice-Recteur chargé des Affaires Académique';
            break;
        case 'secretaire':
            $final = 'Sécrétaire';
            break;
        case 'chef-institution':
            $final = 'Chef d\'institution';
            break;
        default:
            $final = '';
            break;
    }
    return $final;
}

function generateRequestCode()
{
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

    // Générer 3 premiers caractères
    $part1 = substr(str_shuffle($chars), 0, 3);

    // Générer 3 derniers caractères
    $part2 = substr(str_shuffle($chars), 0, 3);

    return $part1 . '-' . $part2;
}

function findService($service): string
{
    $authority = "";
    if ($service == "0") {
        $authority = "Recteur";
    } elseif ($service == "1") {
        $authority = "Vice-Recteur chargé des Affaires Académiques";
    } elseif ($service == "2") {
        $authority = "Vice-Recteur chargé de la Recherche Universitaire";
    } elseif ($service == "3") {
        $authority = "Vice-Recteur chargé de la Coopération";
    }
    return $authority;
}



function array_duplicates(array $items): array
{
    return array_values(
        array_unique(
            array_diff_assoc($items, array_unique($items))
        )
    );
}


function appointmentLevel(string $level): array
{
    return match ($level) {
        'low' => [
            'label' => 'Faible',
            'class' => 'bg-success',
            'icon' => 'bi-arrow-down-circle',
        ],
        'medium' => [
            'label' => 'Moyen',
            'class' => 'bg-warning text-dark',
            'icon' => 'bi-exclamation-circle',
        ],
        'high' => [
            'label' => 'Élevé',
            'class' => 'bg-danger',
            'icon' => 'bi-fire',
        ],
        default => [
            'label' => '—',
            'class' => 'bg-secondary',
            'icon' => 'bi-question-circle',
        ],
    };
}


function editReport(string $status)
{
    $message = "";
    if ($status == "pending") {
        $message = "Modifier le rapport";
    } elseif ($status == "rejected") {
        $message = "Rectifier et soumettre le rapport";
    }
    return $message;
}

function fileIcon($mime) {
    return match (true) {
        str_contains($mime, 'pdf') => 'bi bi-file-earmark-pdf',
        str_contains($mime, 'image') => 'bi bi-file-earmark-image',
        str_contains($mime, 'word') => 'bi bi-file-earmark-word',
        str_contains($mime, 'excel') => 'bi bi-file-earmark-excel',
        default => 'bi bi-file-earmark-text',
    };
}

function fileColorClass($mime) {
    return match (true) {
        str_contains($mime, 'pdf') => 'bg-danger bg-opacity-10 text-danger',
        str_contains($mime, 'image') => 'bg-info bg-opacity-10 text-info',
        str_contains($mime, 'word') => 'bg-primary bg-opacity-10 text-primary',
        str_contains($mime, 'excel') => 'bg-success bg-opacity-10 text-success',
        default => 'bg-secondary bg-opacity-10 text-secondary',
    };
}