<?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\output;

use local_kid\local\eval_repository;

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

class evaluations_admin_page implements \renderable, \templatable {

    /** @var int */
    protected $promotionid;

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

    public function export_for_template(\renderer_base $output): array {
        $evals = eval_repository::get_for_promotion($this->promotionid);

        $rows = [];
        foreach ($evals as $eval) {
            $competencies = eval_repository::get_competencies((int) $eval->id);
            $ismoodledevoir = $eval->sourcetype === 'moodle_devoir';

            $row = [
                'id' => (int) $eval->id,
                'titre' => format_string($eval->titre),
                'sourcelabel' => $ismoodledevoir
                    ? get_string('eval_sourcetype_devoir', 'local_kid')
                    : get_string('eval_sourcetype_manual', 'local_kid'),
                'competencycount' => count($competencies),
                'statuslabel' => $eval->active ? get_string('competency_active', 'local_kid') : get_string('promotion_status_inactive', 'local_kid'),
                'statusclass' => $eval->active ? 'fr-badge fr-badge--success' : 'fr-badge',
                'editurl' => (new \moodle_url('/local/kid/pages/eval_edit.php', ['id' => $eval->id, 'promotionid' => $this->promotionid]))->out(false),
                'toggleurl' => (new \moodle_url('/local/kid/pages/eval_action.php',
                    ['id' => $eval->id, 'promotionid' => $this->promotionid, 'action' => 'toggle', 'sesskey' => sesskey()]))->out(false),
                'deleteurl' => (new \moodle_url('/local/kid/pages/eval_action.php',
                    ['id' => $eval->id, 'promotionid' => $this->promotionid, 'action' => 'delete']))->out(false),
                'openurl' => (new \moodle_url('/local/kid/pages/evaluations.php', ['evalid' => $eval->id]))->out(false),
                'ismoodledevoir' => $ismoodledevoir,
            ];

            if ($ismoodledevoir) {
                $entries = eval_repository::get_entries_for_eval((int) $eval->id);
                $changedcount = 0;
                $lastsync = 0;
                foreach ($entries as $entry) {
                    if (!empty($entry->feedbackchanged)) {
                        $changedcount++;
                    }
                    if ($entry->lastsyncat) {
                        $lastsync = max($lastsync, (int) $entry->lastsyncat);
                    }
                }
                $row['synclabel'] = $changedcount > 0
                    ? get_string('eval_sync_changed_count', 'local_kid', $changedcount)
                    : get_string('eval_sync_uptodate', 'local_kid');
                $row['syncclass'] = $changedcount > 0 ? 'fr-badge fr-badge--warning' : 'fr-badge fr-badge--success';
                $row['lastsynclabel'] = $lastsync ? userdate($lastsync, get_string('strftimedatetimeshort', 'core_langconfig')) : get_string('eval_sync_never', 'local_kid');
                $row['syncurl'] = (new \moodle_url('/local/kid/pages/eval_sync.php',
                    ['id' => $eval->id, 'promotionid' => $this->promotionid, 'sesskey' => sesskey()]))->out(false);
            }

            $rows[] = $row;
        }

        return [
            'promotionid' => $this->promotionid,
            'addurl' => (new \moodle_url('/local/kid/pages/eval_edit.php', ['promotionid' => $this->promotionid]))->out(false),
            'hasevals' => !empty($rows),
            'evals' => $rows,
            'sesskey' => sesskey(),
        ];
    }
}
