<?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();

/**
 * The 8 sub-tabs of pages/admin.php. Single source of truth so the tab
 * strip and the dispatcher's switch never drift — each phase adds its own
 * entry here and its own case in pages/admin.php, nothing else changes.
 * Journal joined this list as its last tab (moved from a top-level nav
 * entry) — reuses journal_page unchanged, see pages/admin.php's 'journal'
 * case and pages/journal.php's redirect stub.
 */
class admin_tabs {

    /** @var string[] key => lang string key, in display order. */
    const TABS = [
        'promotions' => 'admintab_promotions',
        'competences' => 'admintab_competences',
        'evaluations' => 'admintab_evaluations',
        'stages' => 'admintab_stages',
        'capp' => 'admintab_capp',
        'niveaux' => 'admintab_niveaux',
        'export' => 'admintab_export',
        'journal' => 'admintab_journal',
    ];

    /** @var string[] key => local/kid:xxx capability suffix gating that tab. */
    const CAPABILITIES = [
        'promotions' => 'manage',
        'competences' => 'manage',
        'evaluations' => 'manage_evaluation',
        'stages' => 'manage_stage',
        'capp' => 'manage_capp',
        'niveaux' => 'manage',
        'export' => 'export_adesi',
        'journal' => 'viewjournal',
    ];

    /**
     * @param string $tab one of the TABS keys
     * @return string local/kid:xxx capability
     */
    public static function get_capability(string $tab): string {
        return 'local/kid:' . self::CAPABILITIES[$tab];
    }

    /**
     * First tab (in TABS order) the current user actually has the
     * capability for, so landing on the default tab never itself throws a
     * required_capability_exception for a scoped admin role (e.g. a
     * manage_stage-only holder defaults to "stages", not "promotions").
     */
    public static function get_default(): string {
        $context = \context_system::instance();
        foreach (self::TABS as $key => $stringkey) {
            if (access_helper::has_capability_or_teacher(self::get_capability($key), $context)) {
                return $key;
            }
        }
        return 'promotions';
    }

    public static function is_valid(string $tab): bool {
        return array_key_exists($tab, self::TABS);
    }

    /**
     * @param string $activetab
     * @return array items ready for templates/tabs.mustache — only tabs the
     *  current user holds the capability for are included
     */
    public static function get_tab_items(string $activetab): array {
        $context = \context_system::instance();
        $items = [];
        foreach (self::TABS as $key => $stringkey) {
            if (!access_helper::has_capability_or_teacher(self::get_capability($key), $context)) {
                continue;
            }
            $items[] = [
                'label' => get_string($stringkey, 'local_kid'),
                'url' => (new \moodle_url('/local/kid/pages/admin.php', ['tab' => $key]))->out(false),
                'isactive' => $key === $activetab,
            ];
        }
        return $items;
    }
}
