<?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.
// 
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Entity class for DTA submission plugin recommendation
 *
 * @package    assignsubmission_dta
 * @copyright  2023 Gero Lueckemeyer
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace assignsubmission_dta\models;

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

/**
 * Entity class for DTA submission plugin recommendation
 *
 * @package    assignsubmission_dta
 * @copyright  2023
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class dta_recommendation {

    /**
     * @var string $topic Topic of the recommendation.
     */
    public $topic;

    /**
     * @var string $exercise_name Name of the exercise.
     */
    public $exercise_name;

    /**
     * @var string $url URL of the exercise.
     */
    public $url;

    /**
     * @var int $difficulty Difficulty level of the exercise.
     */
    public $difficulty;

    /**
     * @var int $score Score associated with the recommendation.
     */
    public $score;

    /**
     * Decodes the JSON recommendations returned by the backend service call into an array of DtaRecommendation objects.
     *
     * @param string $jsonstring JSON string containing recommendations
     * @return array Array of DtaRecommendation objects
     */
    public static function assignsubmission_dta_decode_json_recommendations(string $jsonstring): array {
        $response = json_decode($jsonstring);

        $recommendations = [];

        // Prüfe, ob Empfehlungen vorhanden sind
        if (!empty($response->recommendations)) {
            foreach ($response->recommendations as $recommendation) {
                $rec = new dta_recommendation();
                $rec->topic = $recommendation->topic ?? null;
                $rec->exercise_name = $recommendation->url ?? null;
                $rec->url = $recommendation->exerciseName ?? null;
                $rec->difficulty = $recommendation->difficulty ?? null;
                $rec->score = $recommendation->score ?? null;

                $recommendations[] = $rec;
            }
        }

        return $recommendations;
    }
}