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

/**
 * Simulatore contabilità
 *
 * @package    mod_mtcontabilita
 * @copyright  2026 Mediatouch 2000 Srl <https://www.mediatouch.it>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Marco Cappuccio <m.cappuccio@mediatouch.it>
 */

/**
 * Restore mtcontabilita structure, with no userinfo.
 *
 * Special rule for mtcontabilita_list:
 * - do NOT duplicate list rows inside the destination course
 * - reuse if exists (match: courseid + name + lang + scheme)
 * - insert only if missing
 *
 * mtcontabilita_listmc is never restored (not present in backup).
 */
class restore_mtcontabilita_activity_structure_step extends restore_activity_structure_step {

    /**
     * Define the structure of the restore workflow.
     *
     * @return restore_path_element $structure
     */
    protected function define_structure() {
        $paths = [];

        $paths[] = new restore_path_element('mtcontabilita', '/activity/mtcontabilita');
        $paths[] = new restore_path_element('mtcontabilita_list', '/activity/mtcontabilita/lists/list');
        $paths[] = new restore_path_element('mtcontabilita_itemdet', '/activity/mtcontabilita/itemdets/itemdet');

        return $this->prepare_activity_structure($paths);
    }

    /**
     * Process an assign restore.
     *
     * @param object $data The data in object form
     * @return void
     */
    protected function process_mtcontabilita($data) {
        global $DB;

        $data = (object)$data;
        $oldid = (int)$data->id;

        // Force destination course.
        $data->course = $this->get_courseid();
        $data->timemodified = time();

        $newid = $DB->insert_record('mtcontabilita', $data);

        $this->apply_activity_instance($newid);
        $this->set_mapping('mtcontabilita', $oldid, $newid);
    }

    /**
     * Restore list row with de-dup inside destination course.
     */
    protected function process_mtcontabilita_list($data) {
        global $DB;

        $data = (object)$data;
        $oldid = (int)$data->id;
        $destcourseid = $this->get_courseid();

        // Logical identity (as agreed).
        $match = [
            'courseid' => $destcourseid,
            'name'     => $data->name,
            'lang'     => $data->lang,
            'scheme'   => $data->scheme,
        ];

        $existing = $DB->get_record('mtcontabilita_list', $match);

        if ($existing) {
            $newid = (int)$existing->id;
        } else {
            $ins = new stdClass();
            $ins->courseid     = $destcourseid;
            $ins->hidden       = $data->hidden ?? 0;
            $ins->name         = $data->name;
            $ins->type         = $data->type;
            $ins->nature       = $data->nature ?? 0;
            $ins->scheme       = $data->scheme;
            $ins->macroclasse  = $data->macroclasse;
            $ins->lang         = $data->lang;
            $ins->usercreated  = $data->usercreated ?? 0;
            $ins->timecreated  = $data->timecreated ?? time();

            $newid = $DB->insert_record('mtcontabilita_list', $ins);
        }

        // Map old list id -> destination list id.
        $this->set_mapping('mtcontabilita_list', $oldid, $newid);
    }

    /**
     * Restore list row with de-dup inside destination course.
     */
    protected function process_mtcontabilita_itemdet($data) {
        global $DB;

        $data = (object)$data;
        $oldid = (int)$data->id;

        $data->mtcontabilitaid = $this->get_new_parentid('mtcontabilita');

        // Remap listid if set (default is 0).
        if (!empty($data->listid)) {
            $data->listid = $this->get_mappingid('mtcontabilita_list', $data->listid);
        }

        $newid = $DB->insert_record('mtcontabilita_itemdet', $data);
        $this->set_mapping('mtcontabilita_itemdet', $oldid, $newid);
    }

    /**
     * Once the database tables have been fully restored, restore the files
     */
    protected function after_execute() {
        $this->add_related_files('mod_mtcontabilita', 'intro', null);
        $this->add_related_files('mod_mtcontabilita', 'description', null);
    }
}
