<?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.

/**
 * Onglet admin "CAPP" : CRUD des sessions + panneau de rattachement des
 * évaluations/stages.
 *
 * @package    local_kid
 */

namespace local_kid\output;

use local_kid\local\capp_repository;
use local_kid\local\eval_repository;
use local_kid\local\stage_repository;

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

class capp_admin_page implements \renderable, \templatable {

    protected int $promotionid;

    public function __construct(int $promotionid) {
        $this->promotionid = $promotionid;
    }

    public function export_for_template(\renderer_base $output): array {
        $sessions = capp_repository::get_sessions_for_promotion($this->promotionid);

        $rows = [];
        $sessionoptions = [['id' => 0, 'label' => get_string('capp_unassigned', 'local_kid')]];
        foreach ($sessions as $session) {
            $rows[] = [
                'id' => (int) $session->id,
                'code' => format_string($session->code),
                'libelle' => format_string($session->libelle),
                'statuslabel' => $session->active ? get_string('promotion_status_active', 'local_kid') : get_string('promotion_status_inactive', 'local_kid'),
                'statusclass' => $session->active ? 'fr-badge fr-badge--success' : 'fr-badge',
                'editurl' => (new \moodle_url('/local/kid/pages/capp_edit.php', ['id' => $session->id, 'promotionid' => $this->promotionid]))->out(false),
                'toggleurl' => (new \moodle_url('/local/kid/pages/capp_action.php',
                    ['id' => $session->id, 'promotionid' => $this->promotionid, 'action' => 'toggle', 'sesskey' => sesskey()]))->out(false),
                'deleteurl' => (new \moodle_url('/local/kid/pages/capp_action.php',
                    ['id' => $session->id, 'promotionid' => $this->promotionid, 'action' => 'delete']))->out(false),
                'openurl' => (new \moodle_url('/local/kid/pages/capp.php', ['promotionid' => $this->promotionid, 'sessionid' => $session->id]))->out(false),
            ];
            $sessionoptions[] = ['id' => (int) $session->id, 'label' => format_string($session->code)];
        }

        $rattachrows = [];
        foreach (eval_repository::get_for_promotion($this->promotionid) as $eval) {
            if (!$eval->active) {
                continue;
            }
            $rattachrows[] = $this->rattach_row('eval', (int) $eval->id, format_string($eval->titre), $sessionoptions);
        }
        foreach (stage_repository::get_for_promotion($this->promotionid) as $stage) {
            if (!$stage->active) {
                continue;
            }
            $rattachrows[] = $this->rattach_row('stage', (int) $stage->id, format_string($stage->libelle), $sessionoptions);
        }

        return [
            'promotionid' => $this->promotionid,
            'addurl' => (new \moodle_url('/local/kid/pages/capp_edit.php', ['promotionid' => $this->promotionid]))->out(false),
            'hassessions' => !empty($rows),
            'sessions' => $rows,
            'hasrattachable' => !empty($rattachrows),
            'rattachrows' => $rattachrows,
            'rattachurl' => (new \moodle_url('/local/kid/pages/capp_rattach.php'))->out(false),
            'sesskey' => sesskey(),
            'hassessionsforrule' => !empty($rows),
        ];
    }

    private function rattach_row(string $sourcetype, int $sourceid, string $label, array $sessionoptions): array {
        $assigned = capp_repository::get_source_assignment($sourcetype, $sourceid, $this->promotionid);
        $options = [];
        foreach ($sessionoptions as $opt) {
            $options[] = ['id' => $opt['id'], 'label' => $opt['label'], 'selected' => $opt['id'] == ($assigned ?? 0)];
        }
        return [
            'sourcetype' => $sourcetype,
            'sourceid' => $sourceid,
            'label' => $label,
            'typelabel' => $sourcetype === 'eval' ? get_string('admintab_evaluations', 'local_kid') : get_string('admintab_stages', 'local_kid'),
            'options' => $options,
        ];
    }
}
