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

use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use local_kid\local\access_helper;
use local_kid\local\dashboard_favori_repository;

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

/**
 * Deletes one of the calling user's own dashboard favoris — same ownership
 * guarantee as {@see dashboard_favori_rename}, enforced in
 * {@see dashboard_favori_repository::delete()}.
 */
class dashboard_favori_delete extends external_api {

    public static function execute_parameters(): external_function_parameters {
        return new external_function_parameters([
            'id' => new external_value(PARAM_INT, 'local_kid_dashboard_favori.id'),
        ]);
    }

    public static function execute(int $id): array {
        global $USER;

        ['id' => $id] = self::validate_parameters(self::execute_parameters(), ['id' => $id]);

        $context = \context_system::instance();
        self::validate_context($context);
        access_helper::require_capability_or_teacher('local/kid:managedashboardfavourites', $context);

        $success = dashboard_favori_repository::delete($id, (int) $USER->id);

        return ['success' => $success];
    }

    public static function execute_returns(): external_single_structure {
        return new external_single_structure([
            'success' => new external_value(PARAM_BOOL, 'False if the favori does not exist or is not owned by the caller'),
        ]);
    }
}
