<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

namespace local_kid\local;

defined('MOODLE_INTERNAL') || die();

/**
 * Single source of truth for the plugin's 9 navigation entries, shared by
 * lib.php's local_kid_extend_navigation() (Moodle's own nav tree) and the
 * in-page sidebar rendered on every local_kid screen (nav_sidebar.mustache),
 * so the two never drift.
 */
class nav_helper {

    /**
     * @var array Ordered list of [capability suffixes (ANY-of), lang string key, relative url, node key, DSFR icon class].
     *
     * Journal and Comparateur are deliberately absent: both moved from
     * top-level nav entries into tabs of an existing screen (Journal ->
     * Administration, Comparateur -> Tableau de bord) — see pages/journal.php
     * and pages/comparateur.php, now thin redirect stubs to those tabs.
     */
    const ENTRIES = [
        [['view'], 'nav_dashboard', '/local/kid/pages/dashboard.php', 'dashboard', 'fr-icon-dashboard-3-line'],
        [['view'], 'nav_evaluations', '/local/kid/pages/evaluations.php', 'evaluations', 'fr-icon-book-2-line'],
        [['view'], 'nav_stages', '/local/kid/pages/stages.php', 'stages', 'fr-icon-briefcase-line'],
        [['view'], 'nav_capp', '/local/kid/pages/capp.php', 'capp', 'fr-icon-team-line'],
        [['viewown'], 'nav_student', '/local/kid/pages/student.php', 'student', 'fr-icon-user-line'],
        [['viewannuaire'], 'nav_annuaire', '/local/kid/pages/annuaire.php', 'annuaire', 'fr-icon-user-search-line'],
        [['manage_evaluation', 'manage_stage', 'manage_capp', 'manage', 'export_adesi', 'viewjournal'], 'nav_admin', '/local/kid/pages/admin.php', 'admin', 'fr-icon-settings-5-line'],
    ];

    /**
     * @param string[] $capabilities local/kid:xxx suffixes, ANY of which grants access
     * @return bool
     */
    public static function can_access_entry(array $capabilities): bool {
        $context = \context_system::instance();
        foreach ($capabilities as $capability) {
            if (access_helper::has_capability_or_teacher('local/kid:' . $capability, $context)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param string|null $activekey one of the ENTRIES node keys, or null
     * @return array items ready for templates/nav_sidebar.mustache
     */
    public static function get_sidebar_items(?string $activekey = null): array {
        $items = [];

        foreach (self::ENTRIES as [$capabilities, $stringkey, $url, $key, $icon]) {
            if (!self::can_access_entry($capabilities)) {
                continue;
            }
            $items[] = [
                'label' => get_string($stringkey, 'local_kid'),
                'url' => (new \moodle_url($url))->out(false),
                'isactive' => $key === $activekey,
                'icon' => $icon,
            ];
        }

        return $items;
    }

    /**
     * Whether this user should see/use the global promotion switcher
     * (sélecteur A, templates/nav_sidebar.mustache).
     */
    public static function can_switch_promotion(): bool {
        $context = \context_system::instance();
        return access_helper::has_capability_or_teacher('local/kid:selectpromotion', $context);
    }
}
