<?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\promotion_repository;
use local_kid\local\promotion_selector;

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

class promotions_admin_page implements \renderable, \templatable {

    public function export_for_template(\renderer_base $output): array {
        global $USER;

        $promotions = promotion_repository::get_all();
        $cohorts = promotion_repository::get_all_moodle_cohorts();
        $displayed = promotion_selector::resolve($USER->id);
        $displayedid = $displayed ? (int) $displayed->id : 0;
        $morethanone = count($promotions) > 1;

        $rows = [];
        foreach ($promotions as $promotion) {
            $islinked = !empty($promotion->moodlecohortid);
            $isdisplayed = $displayedid === (int) $promotion->id;
            $candelete = $morethanone && !$isdisplayed;
            $rows[] = [
                'id' => (int) $promotion->id,
                'name' => format_string($promotion->name),
                'cohortlabel' => $islinked
                    ? ($cohorts[(int) $promotion->moodlecohortid] ?? get_string('promotion_cohort_deleted', 'local_kid'))
                    : get_string('promotion_cohort_none', 'local_kid'),
                'islinked' => $islinked,
                'studentcount' => promotion_repository::get_active_student_count((int) $promotion->id),
                'statuslabel' => $promotion->active
                    ? get_string('promotion_status_active', 'local_kid')
                    : get_string('promotion_status_inactive', 'local_kid'),
                'statusclass' => $promotion->active ? 'fr-badge fr-badge--success' : 'fr-badge',
                'renameurl' => (new \moodle_url('/local/kid/pages/promotion_edit.php', ['id' => $promotion->id]))->out(false),
                'linkurl' => (new \moodle_url('/local/kid/pages/promotion_link.php', ['id' => $promotion->id]))->out(false),
                'unlinkurl' => (new \moodle_url('/local/kid/pages/promotion_action.php',
                    ['id' => $promotion->id, 'action' => 'unlink', 'sesskey' => sesskey()]))->out(false),
                'resyncurl' => (new \moodle_url('/local/kid/pages/promotion_action.php',
                    ['id' => $promotion->id, 'action' => 'resync', 'sesskey' => sesskey()]))->out(false),
                'toggleurl' => (new \moodle_url('/local/kid/pages/promotion_action.php',
                    ['id' => $promotion->id, 'action' => 'toggle', 'sesskey' => sesskey()]))->out(false),
                'deleteurl' => (new \moodle_url('/local/kid/pages/promotion_action.php',
                    ['id' => $promotion->id, 'action' => 'delete']))->out(false),
                'candelete' => $candelete,
                'deleteblockedreason' => !$morethanone
                    ? get_string('promotion_delete_blocked_last', 'local_kid')
                    : ($isdisplayed ? get_string('promotion_delete_blocked_current', 'local_kid') : ''),
            ];
        }

        return [
            'addurl' => (new \moodle_url('/local/kid/pages/promotion_edit.php'))->out(false),
            'haspromotions' => !empty($rows),
            'promotions' => $rows,
            'sesskey' => sesskey(),
        ];
    }
}
