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

/**
 * Actions ponctuelles sur une promotion : délier / resynchroniser / activer
 * (dé)sactiver. `managecohorts` pour délier/resync, `manage` pour le
 * toggle actif/inactif — capacités volontairement différenciées.
 *
 * @package    local_kid
 */

require(__DIR__ . '/../../../config.php');

use local_kid\local\access_helper;
use local_kid\local\promotion_repository;
use local_kid\local\promotion_selector;

require_login();
$context = context_system::instance();

$id = required_param('id', PARAM_INT);
$action = required_param('action', PARAM_ALPHA);
require_sesskey();

$promotion = promotion_repository::get($id);
if (!$promotion) {
    throw new \moodle_exception('invalidrecord', 'error');
}

$returnurl = new moodle_url('/local/kid/pages/admin.php', ['tab' => 'promotions']);

switch ($action) {
    case 'unlink':
        access_helper::require_capability_or_teacher('local/kid:manage', $context);
        promotion_repository::unlink_cohort($id, $USER->id);
        redirect($returnurl, get_string('promotion_unlinked', 'local_kid'), null, \core\output\notification::NOTIFY_SUCCESS);
        break;

    case 'resync':
        access_helper::require_capability_or_teacher('local/kid:manage', $context);
        $result = promotion_repository::resync($id, $USER->id);
        redirect($returnurl, get_string('promotion_link_result', 'local_kid', (object) $result), null, \core\output\notification::NOTIFY_SUCCESS);
        break;

    case 'toggle':
        access_helper::require_capability_or_teacher('local/kid:manage', $context);
        promotion_repository::toggle_active($id, $USER->id);
        redirect($returnurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
        break;

    case 'delete':
        access_helper::require_capability_or_teacher('local/kid:manage', $context);
        if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
            throw new \moodle_exception('invalidrequest', 'error');
        }
        if (!promotion_repository::get($id)) {
            throw new \moodle_exception('invalidrecord', 'error');
        }
        if (count(promotion_repository::get_all()) <= 1) {
            redirect($returnurl, get_string('promotion_delete_blocked_last', 'local_kid'), null, \core\output\notification::NOTIFY_ERROR);
        }
        $displayed = promotion_selector::resolve($USER->id);
        if ($displayed && (int) $displayed->id === $id) {
            redirect($returnurl, get_string('promotion_delete_blocked_current', 'local_kid'), null, \core\output\notification::NOTIFY_ERROR);
        }
        promotion_repository::delete($id, $USER->id);
        redirect($returnurl, get_string('promotion_deleted', 'local_kid'), null, \core\output\notification::NOTIFY_SUCCESS);
        break;

    default:
        throw new \moodle_exception('invalidparameter', 'error');
}
