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

/**
 * Builds the student-navigation header (selector + previous/next + position
 * counter) shared by the three data-entry screens (Évaluations, Stages,
 * CAPP) once a student is selected — {@see templates/student_nav.mustache}.
 * Kept in one place so all three screens stay identical and a student
 * switch (selector, prev/next, or the AJAX fragment reload in
 * amd/src/student_nav.js) is always computed the same way.
 */
class student_nav_helper {

    /** @var array<string, array{controller:string, itemparam:string}> */
    private const SCREENS = [
        'evaluations' => ['controller' => 'evaluations.php', 'itemparam' => 'evalid'],
        'stages' => ['controller' => 'stages.php', 'itemparam' => 'stageid'],
        'capp' => ['controller' => 'capp.php', 'itemparam' => 'sessionid'],
    ];

    public static function is_valid_screen(string $screen): bool {
        return isset(self::SCREENS[$screen]);
    }

    /**
     * @param string $screen one of 'evaluations', 'stages', 'capp'
     * @param int $itemid the eval/stage/session id (unchanged while navigating students)
     * @param int $promotionid
     * @param int $studentid the currently displayed student (local_kid_student.id)
     * @return array template context for templates/student_nav.mustache
     */
    public static function build(string $screen, int $itemid, int $promotionid, int $studentid): array {
        if (!self::is_valid_screen($screen)) {
            throw new \coding_exception('Unknown local_kid student nav screen: ' . $screen);
        }
        $config = self::SCREENS[$screen];

        $students = array_values(promotion_repository::get_active_students($promotionid));
        $total = count($students);

        $options = [];
        $position = 0;
        $previd = null;
        $nextid = null;
        foreach ($students as $i => $student) {
            $isselected = (int) $student->studentid === $studentid;
            $options[] = [
                'id' => (int) $student->studentid,
                'name' => $student->lastname . ' ' . $student->firstname,
                'selected' => $isselected,
            ];
            if ($isselected) {
                $position = $i + 1;
                $previd = $i > 0 ? (int) $students[$i - 1]->studentid : null;
                $nextid = $i < $total - 1 ? (int) $students[$i + 1]->studentid : null;
            }
        }

        return [
            'screen' => $screen,
            'itemid' => $itemid,
            'itemparam' => $config['itemparam'],
            'formaction' => (new \moodle_url('/local/kid/pages/' . $config['controller']))->out(false),
            'options' => $options,
            'hasoptions' => $total > 0,
            'hasprev' => $previd !== null,
            'hasnext' => $nextid !== null,
            'prevstudentid' => $previd,
            'nextstudentid' => $nextid,
            'prevurl' => $previd !== null ? self::url($config, $itemid, $previd) : '',
            'nexturl' => $nextid !== null ? self::url($config, $itemid, $nextid) : '',
            'position' => $position,
            'total' => $total,
            'counterlabel' => get_string('student_nav_counter', 'local_kid', (object) [
                'position' => $position, 'total' => $total,
            ]),
        ];
    }

    private static function url(array $config, int $itemid, int $studentid): string {
        return (new \moodle_url('/local/kid/pages/' . $config['controller'], [
            $config['itemparam'] => $itemid,
            'studentid' => $studentid,
        ]))->out(false);
    }
}
