<?php
declare(strict_types=1);
namespace App\Models;

use App\Core\Database;
use PDO;
use Throwable;

final class DirectionTask
{
    public static function all(int $organisationId): array
    {
        $pdo=Database::connection();
        $tasks=[];
        try {
            $q=$pdo->prepare(
                "SELECT p.id,p.public_token,p.nom,p.prenom,COUNT(s.id) total
                 FROM cda_professor_slots s
                 INNER JOIN professeurs p ON p.id=s.professeur_id
                 WHERE s.organisation_id=? AND s.deleted_at IS NULL
                   AND s.statut='brouillon' AND p.deleted_at IS NULL
                 GROUP BY p.id,p.public_token,p.nom,p.prenom ORDER BY p.nom,p.prenom"
            );
            $q->execute([$organisationId]);
            foreach($q->fetchAll(PDO::FETCH_ASSOC) as$row)$tasks[]=[
                'kind'=>'planning','title'=>'Planning professeur à valider',
                'text'=>$row['prenom'].' '.$row['nom'].' · '.(int)$row['total'].' créneau(x)',
                'url'=>'/public/professeurs-pro/planning?professeur='.rawurlencode((string)$row['public_token']).'&mode=direction'
            ];
        } catch (Throwable) {}
        try {
            $q=$pdo->prepare(
                "SELECT rw.professeur_id,p.public_token,p.nom,p.prenom,COUNT(*) total
                 FROM cda_professor_remote_work rw
                 INNER JOIN professeurs p ON p.id=rw.professeur_id
                 WHERE rw.organisation_id=? AND rw.statut='envoye' AND p.deleted_at IS NULL
                 GROUP BY rw.professeur_id,p.public_token,p.nom,p.prenom ORDER BY MIN(rw.date_teletravail)"
            );
            $q->execute([$organisationId]);
            foreach($q->fetchAll(PDO::FETCH_ASSOC) as$row)$tasks[]=[
                'kind'=>'teletravail','title'=>'Télétravail à valider',
                'text'=>$row['prenom'].' '.$row['nom'].' · '.(int)$row['total'].' compte(s) rendu(s)',
                'url'=>'/public/professeurs-pro/teletravail?professeur='.rawurlencode((string)$row['public_token']).'&mode=direction'
            ];
        } catch (Throwable) {}
        try {
            $q=$pdo->prepare(
                "SELECT r.professeur_id,p.public_token,p.nom,p.prenom,COUNT(*) total
                 FROM cda_professor_planning_move_requests r
                 INNER JOIN professeurs p ON p.id=r.professeur_id
                 WHERE r.organisation_id=? AND r.statut='en_attente' AND p.deleted_at IS NULL
                 GROUP BY r.professeur_id,p.public_token,p.nom,p.prenom ORDER BY MIN(r.created_at)"
            );
            $q->execute([$organisationId]);
            foreach($q->fetchAll(PDO::FETCH_ASSOC) as$row)$tasks[]=[
                'kind'=>'deplacement','title'=>'Déplacement à décider',
                'text'=>$row['prenom'].' '.$row['nom'].' · '.(int)$row['total'].' demande(s)',
                'url'=>'/public/professeurs-pro/planning?professeur='.rawurlencode((string)$row['public_token']).'&mode=direction'
            ];
        } catch (Throwable) {}
        return $tasks;
    }

    public static function count(int $organisationId): int
    {
        return count(self::all($organisationId));
    }
}
