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

/**
 * Define the complete mtcontabilita structure for backup, with no userinfo.
 *
 * Includes:
 * - mtcontabilita (activity settings)
 * - mtcontabilita_itemdet (teacher-defined rows)
 * - mtcontabilita_list ONLY for list entries referenced by itemdet.listid
 *
 * Excludes:
 * - mtcontabilita_attempts
 * - mtcontabilita_attempt_items
 * - mtcontabilita_listmc (never backed up)
 */
class backup_mtcontabilita_activity_structure_step extends backup_activity_structure_step {

    /**
     * Define the structure for the assign activity
     * @return backup_nested_element
     */
    protected function define_structure() {
        $mtc = new backup_nested_element('mtcontabilita', ['id'], [
            'course', 'name', 'intro', 'introformat', 'type', 'inline', 'attempt',
            'timelimit', 'timeopen', 'timeclose', 'anonymous', 'binit', 'bmacroclasse',
            'bdecimali', 'slang', 'publish', 'resume', 'calcolo', 'description',
            'descriptionformat', 'options', 'completionsubmit', 'timemodified',
        ]);

        // Itemdets.
        $itemdets = new backup_nested_element('itemdets');
        $itemdet  = new backup_nested_element('itemdet', ['id'], [
            'itemdesc', 'itemdescformat', 'itemnum', 'itemdate', 'sort', 'sortorder',
            'listid', 'itemtype', 'amount', 'score', 'type',
        ]);

        // Only list rows referenced by this activity's itemdets.
        $lists = new backup_nested_element('lists');
        $list  = new backup_nested_element('list', ['id'], [
            'hidden', 'name', 'type', 'nature', 'scheme', 'macroclasse', 'lang',
            'usercreated', 'timecreated',
        ]);

        // Build tree (IMPORTANT: lists first, so list mappings exist when itemdets are restored).
        $mtc->add_child($lists);
        $lists->add_child($list);

        $mtc->add_child($itemdets);
        $itemdets->add_child($itemdet);

        // Sources.
        $mtc->set_source_table('mtcontabilita', ['id' => backup::VAR_ACTIVITYID]);

        $itemdet->set_source_table('mtcontabilita_itemdet', [
            'mtcontabilitaid' => backup::VAR_PARENTID,
        ]);

        // Only the list entries referenced by itemdet.listid (ignore listid=0).
        $list->set_source_sql("
            SELECT DISTINCT l.*
              FROM {mtcontabilita_list} l
              JOIN {mtcontabilita_itemdet} d ON d.listid = l.id
             WHERE d.mtcontabilitaid = :mtcid
        ", ['mtcid' => backup::VAR_ACTIVITYID]);

        // Files (standard file areas).
        $mtc->annotate_files('mod_mtcontabilita', 'intro', null);
        $mtc->annotate_files('mod_mtcontabilita', 'description', null);

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