<?php
// This file is part of the Studyplan plugin for Moodle
//
// 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 <https://www.gnu.org/licenses/>.
/**
 * Class to manage connection lines between study items
 * @package    local_treestudyplan
 * @copyright  2023 P.M. Kuipers
 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

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

require_once($CFG->libdir.'/externallib.php');

/**
 * Class to manage connection lines between study items
 */
class studyitemconnection {

    /**
     * Database table for this model
     * @var string */
    const TABLE = "local_treestudyplan_connect";

    /**
     * Holds database record
     * @var stdClass
     */
    private $r;
    /** @var int */
    private $id;

    /**
     * Construct new object based on database object
     * @param stdClass $r Database record
     */
    protected function __construct($r) {
        $this->r = $r;
        $this->id = $r->id;
    }

    /**
     * Webservice structure for basic info
     * @param int $value Webservice requirement constant
     */
    public static function structure($value = VALUE_REQUIRED) : \external_description {
        return new \external_single_structure([
            'id' => new \external_value(PARAM_INT, 'id of connection'),
            'from_id' => new \external_value(PARAM_INT, 'id of start item'),
            'to_id' => new \external_value(PARAM_INT, 'id of end item'),
        ], '', $value);
    }

    /**
     * Webservice model for basic info
     * @return array Webservice data model
     */
    public function model() {
        return ['id' => $this->r->id, 'from_id' => $this->r->from_id, 'to_id' => $this->r->to_id];
    }


    /**
     * Get originating studyitem for this connection
     */
    public function from_item() : studyitem {
        return studyitem::find_by_id($this->r->from_id);
    }

    /**
     * Get target studyitem for this connection
     */
    public function to_item() : studyitem {
        return studyitem::find_by_id($this->r->to_id);
    }

    /**
     * Get originating studyitem id for this connection
     */
    public function from_id() : int {
        return $this->r->from_id;
    }

    /**
     * Get target studyitem id for this connection
     */
    public function to_id() :int {
        return $this->r->to_id;
    }

    /**
     * Get all connections originating at a given studyitem's id
     * @param int $itemid Id of the studyitem
     * @return studyitemconnection[]
     */
    public static function find_outgoing($itemid) : array {
        global $DB;
        $list = [];
        $connout = $DB->get_records(self::TABLE, ['from_id' => $itemid]);
        foreach ($connout as $c) {
            $list[] = new self($c);
        }
        return $list;
    }

    /**
     * Get all connections terminating at a given studyitem's id
     * @param int $itemid Id of the studyitem
     * @return studyitemconnection[]
     */
    public static function find_incoming($itemid) {
        global $DB;
        $list = [];
        $connin = $DB->get_records(self::TABLE, ['to_id' => $itemid]);
        foreach ($connin as $c) {
            $list[] = new self($c);
        }
        return $list;
    }

    /**
     * Create a new connection between two study items
     * @param int $fromid Id of the originating studyitem
     * @param int $toid Id of the target studyitem
     */
    public static function connect($fromid, $toid) {
        global $DB;

        // Check if link already exists.

        if (!$DB->record_exists(self::TABLE, ['from_id' => $fromid, 'to_id' => $toid])) {
            $id = $DB->insert_record(self::TABLE, [
                'from_id' => $fromid,
                'to_id' => $toid,
            ]);

            return new self($DB->get_record(self::TABLE, ['id' => $id]));

        } else {
            return new self($DB->get_record(self::TABLE, ['from_id' => $fromid, 'to_id' => $toid]));
        }
    }

    /**
     * Delete a connection between two study items
     * @param int $fromid Id of the originating studyitem
     * @param int $toid Id of the target studyitem
     */
    public static function disconnect($fromid, $toid) {
        global $DB;

        if ($DB->record_exists(self::TABLE, ['from_id' => $fromid, 'to_id' => $toid])) {
            $DB->delete_records(self::TABLE, [
                'from_id' => $fromid,
                'to_id' => $toid,
            ]);

            return success::success('Items Disconnected');
        } else {
            return success::success('Connection does not exist');
        }
    }

    /**
     * Clear all connection to and from a given studyitem
     * @param int $id Id of the studyitem
     */
    public static function clear($id) {
        global $DB;
        $DB->delete_records(self::TABLE, ['from_id' => $id]);
        $DB->delete_records(self::TABLE, ['to_id' => $id]);
    }

}