<?php
namespace App\Models;

use App\Core\Database;

final class ParameterManagement
{
    public static function pole(int $id): ?array
    {
        $stmt=Database::connection()->prepare(
            "SELECT *
             FROM poles
             WHERE id=:id
               AND deleted_at IS NULL
             LIMIT 1"
        );
        $stmt->execute(['id'=>$id]);
        return $stmt->fetch() ?: null;
    }

    public static function discipline(int $id): ?array
    {
        $stmt=Database::connection()->prepare(
            "SELECT d.*,p.libelle AS pole_libelle
             FROM disciplines d
             LEFT JOIN poles p ON p.id=d.pole_id
             WHERE d.id=:id
               AND d.deleted_at IS NULL
             LIMIT 1"
        );
        $stmt->execute(['id'=>$id]);
        return $stmt->fetch() ?: null;
    }

    public static function poles(): array
    {
        return Database::connection()->query(
            "SELECT id,code,libelle
             FROM poles
             WHERE actif=1
               AND deleted_at IS NULL
             ORDER BY ordre_affichage,libelle"
        )->fetchAll();
    }

    public static function updatePole(
        int $id,
        string $code,
        string $libelle
    ): void {
        Database::connection()->prepare(
            "UPDATE poles
             SET code=:code,
                 libelle=:libelle,
                 updated_at=CURRENT_TIMESTAMP
             WHERE id=:id"
        )->execute([
            'code'=>$code,
            'libelle'=>$libelle,
            'id'=>$id,
        ]);
    }

    public static function updateDiscipline(
        int $id,
        int $poleId,
        string $code,
        string $libelle,
        int $individualDuration,
        int $collectiveDuration
    ): void {
        Database::connection()->prepare(
            "UPDATE disciplines
             SET pole_id=:pole_id,
                 code=:code,
                 libelle=:libelle,
                 duree_individuelle_minutes=:duree_individuelle_minutes,
                 duree_collective_minutes=:duree_collective_minutes
             WHERE id=:id"
        )->execute([
            'pole_id'=>$poleId,
            'code'=>$code,
            'libelle'=>$libelle,
            'duree_individuelle_minutes'=>$individualDuration,
            'duree_collective_minutes'=>$collectiveDuration,
            'id'=>$id,
        ]);
    }

    public static function poleHasActiveCourses(int $id): bool
    {
        $stmt=Database::connection()->prepare(
            "SELECT COUNT(*)
             FROM ateliers
             WHERE pole_id=:id
               AND deleted_at IS NULL
               AND actif=1"
        );
        $stmt->execute(['id'=>$id]);
        return (int)$stmt->fetchColumn() > 0;
    }

    public static function disciplineHasActiveCourses(int $id): bool
    {
        $stmt=Database::connection()->prepare(
            "SELECT COUNT(*)
             FROM ateliers
             WHERE discipline_id=:id
               AND deleted_at IS NULL
               AND actif=1"
        );
        $stmt->execute(['id'=>$id]);
        return (int)$stmt->fetchColumn() > 0;
    }

    public static function archivePole(int $id): void
    {
        Database::connection()->prepare(
            "UPDATE poles
             SET actif=0,
                 deleted_at=CURRENT_TIMESTAMP,
                 updated_at=CURRENT_TIMESTAMP
             WHERE id=:id"
        )->execute(['id'=>$id]);
    }

    public static function archiveDiscipline(int $id): void
    {
        Database::connection()->prepare(
            "UPDATE disciplines
             SET actif=0,
                 deleted_at=CURRENT_TIMESTAMP
             WHERE id=:id"
        )->execute(['id'=>$id]);
    }

    public static function updatePublic(int $id,string $libelle,?int $ageMin,?int $ageMax): void
    {
        Database::connection()->prepare(
            "UPDATE publics_cibles SET libelle=?,age_min=?,age_max=? WHERE id=? AND deleted_at IS NULL"
        )->execute([$libelle,$ageMin,$ageMax,$id]);
    }
}
