<?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 result summary
 *
 * @package    assignsubmission_dta
 * @copyright  2023 Gero Lueckemeyer and student project teams
 * @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 result summary
 *
 * @package    assignsubmission_dta
 * @copyright  2023 Gero Lueckemeyer and student project teams
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class dta_result_summary {

    /**
     * @var int $timestamp Result timestamp for chronological ordering and deletion of previous results.
     */
    public $timestamp;

    /**
     * @var string $globalstacktrace Global stack trace if applicable, empty string otherwise.
     */
    public $globalstacktrace;

    /**
     * @var string $successfultestcompetencies Successfully tested competencies according to tests and weights, empty string otherwise.
     */
    public $successfultestcompetencies;

    /**
     * @var string $overalltestcompetencies Overall tested competencies according to tests and weights, empty string otherwise.
     */
    public $overalltestcompetencies;

    /**
     * @var array $results List of detail results.
     */
    public $results;

    /**
     * Decodes the JSON result summary returned by the backend service call into the plugin PHP data structure.
     *
     * @param string $jsonstring JSON string containing DtaResultSummary
     * @return DtaResultSummary The result summary
     */
    public static function assignsubmission_dta_decode_json(string $jsonstring): dta_result_summary {
        $response = json_decode($jsonstring);

        $summary = new dta_result_summary();
        $summary->timestamp = $response->timestamp;
        $summary->globalstacktrace = $response->globalstacktrace;

        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile ?? '';
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile ?? '';

        $summary->results = self::assignsubmission_dta_decode_json_result_array($response->results);

        return $summary;
    }

    /**
     * Decodes the array of JSON detail results returned by the backend service call into the plugin PHP data structure.
     *
     * @param array $jsonarray Decoded JSON array of results
     * @return array Array of DtaResult
     */
    private static function assignsubmission_dta_decode_json_result_array(array $jsonarray): array {
        $ret = [];
        foreach ($jsonarray as $entry) {
            $value = new dta_result();
            $value->packagename = $entry->packageName ?? '';
            $value->classname = $entry->className ?? '';
            $value->name = $entry->name ?? '';

            $value->state = $entry->state ?? 0;

            $value->failuretype = $entry->failureType ?? '';
            $value->failurereason = $entry->failureReason ?? '';
            $value->stacktrace = $entry->stacktrace ?? '';

            $value->columnnumber = $entry->columnNumber ?? '';
            $value->linenumber = $entry->lineNumber ?? '';
            $value->position = $entry->position ?? '';

            $ret[] = $value;
        }
        return $ret;
    }

    /**
     * Returns the number of detail results attached to the summary.
     *
     * @return int Count of occurrences
     */
    public function assignsubmission_dta_result_count(): int {
        return count($this->results);
    }

    /**
     * Returns the number of detail results with the given state attached to the summary.
     *
     * @param int $state State ordinal number
     * @return int Count of occurrences for the provided state
     */
    public function assignsubmission_dta_state_occurence_count(int $state): int {
        $num = 0;
        foreach ($this->results as $r) {
            if ($r->state == $state) {
                $num++;
            }
        }
        return $num;
    }

    /**
     * Returns the number of detail results with compilation errors attached to the summary.
     *
     * @return int Count of occurrences
     */
    public function assignsubmission_dta_compilation_error_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(3);
    }

    /**
     * Returns the number of detail results with test failures attached to the summary.
     *
     * @return int Count of occurrences
     */
    public function assignsubmission_dta_failed_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(2);
    }

    /**
     * Returns the number of detail results with successful tests attached to the summary.
     *
     * @return int Count of occurrences
     */
    public function assignsubmission_dta_successful_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(1);
    }

    /**
     * Returns the number of detail results with an unknown result attached to the summary.
     *
     * @return int Count of occurrences
     */
    public function assignsubmission_dta_unknown_count(): int {
        return $this->assignsubmission_dta_state_occurence_count(0);
    }
}