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

namespace mod_mtcontabilita\output;

use renderer_base;

/**
 * Class sessions_list_item
 */
class sessions_list_item implements \templatable, \renderable {

    /**
     * @var \stdClass $session
     */
    private $session;

    /**
     * @var \context_module $context
     */
    private $context;

    /**
     * Constructor
     *
     * @param \stdClass $session
     * @param \stdClass|false $usersubmission
     * @param \context_module $context
     */
    public function __construct(\stdClass $session, $usersubmission, \context_module $context) {
        $this->session = $session;
        $this->session->usersubmission = false;
        // Embed booking submission data if matching the session.
        if ($usersubmission && $this->session->id == $usersubmission->sessionid) {
            $this->session->usersubmission = $usersubmission;
        }
        $this->context = $context;
    }

    /**
     * Exports for template.
     *
     * @param renderer_base $output
     * @return array
     */
    public function export_for_template(renderer_base $output): array {
        global $DB;
        $context = [
            'sessionid' => $this->session->id,
        ];

        // Add date and time.
        $sessiontimes = [];
        foreach ($this->session->sessiondates as $key => $val) {
            $sessiontimes[] = [
                // 'itemdesc' => $val->itemdesc,
                'itemnum' => $val->itemnum,
                'itemdate' => userdate($val->itemdate, get_string('strftimedatefullshort', 'langconfig')),
                'conto' => $DB->get_field('mtcontabilita_list', 'name', ['id' => $val->listid]),
                'itemtype' => $val->itemtype,
                'schema' => $DB->get_field('mtcontabilita_list', 'scheme', ['id' => $val->listid]),
                'amount' => $val->amount,
            ];

        }
        // $sessiontimes[] = ['date' => 22, 'time' => json_encode($this->session->sessiondates), ];
        $context += ['sessiontimes' => $sessiontimes];
        // Add capacity.
        $context += ['capacity' => '22', 'capacitytitle' => '[[bookedvscapacity2]]'];

        // Add status.
        $sessioncontext = new session_status($this->session);
        $context += $sessioncontext->export_for_template($output);

        // Add signup.
        $signupcontext = new session_signup($this->session, $this->context);
        $context += $signupcontext->export_for_template($output);

        // Add description.
        $descriptioncontext = new session_description($this->session, $this->context);
        $context += $descriptioncontext->export_for_template($output);

        // Add actions.
        $actionmenu = $this->action_menu();
        $context['actions'] = $actionmenu->export_for_template($output);
        $context['hasmenu'] = !$actionmenu->is_empty();

        return $context;
    }

    /**
     * Adds an action menu.
     *
     * @return \action_menu
     */
    final protected function action_menu() {
        // Actions.
        $actionmenu = new \action_menu();
        $actionmenu->set_action_label(get_string('actions'));
        $actionmenu->set_boundary('window');

        if (\mod_mtcontabilita\permission::can_edit_sessions($this->context)) {
            $link = new \action_menu_link_secondary(
                new \moodle_url('#'),
                new \pix_icon('t/edit', get_string('settings', 'mtcontabilita')),
                get_string('settings', 'mtcontabilita'),
                ['data-id' => $this->session->id, 'data-action' => 'editsession']);
            $actionmenu->add($link);
        }
        $link = new \action_menu_link_secondary(
            new \moodle_url('#'),
            new \pix_icon('t/delete', get_string('delete', 'mtcontabilita')),
            get_string('delete', 'mtcontabilita'),
            ['data-id' => $this->session->id, 'data-action' => 'deletesession']);
        $actionmenu->add($link);

        return $actionmenu;
    }
}
