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

declare(strict_types=1);

namespace mod_mtcontabilita\reportbuilder\local\entities;

use context_module;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\filters\text;
use core_reportbuilder\local\report\column;
use core_reportbuilder\local\report\filter;
use lang_string;
use stdClass;

/**
 * mtcontabilita class
 */
class mtcontabilita extends base {

    /**
     * tables alias
     */
    protected function get_default_table_aliases(): array {
        return ['mtcontabilita' => 'ap'];
    }

    /**
     * get entity
     */
    protected function get_default_entity_title(): lang_string {
        return new lang_string('mtcontabilita', 'mod_mtcontabilita');
    }

    /**
     * initialise
     */
    public function initialise(): base {
        foreach ($this->get_all_columns() as $column) {
            $this->add_column($column);
        }

        foreach ($this->get_all_filters() as $filter) {
            $this
                ->add_filter($filter)
                ->add_condition($filter);
        }

        return $this;
    }

    /**
     * get columns
     */
    protected function get_all_columns(): array {
        global $DB;

        $mtcontabilita = $this->get_table_alias('mtcontabilita');

        // Column name.
        $columns[] = (new column(
            'name',
            new lang_string('name'),
            $this->get_entity_name()
        ))
            ->add_joins($this->get_joins())
            ->set_type(column::TYPE_TEXT)
            ->add_field("{$mtcontabilita}.name")
            ->set_is_sortable(true)
            ->add_callback(static function(string $value, stdClass $row): string {
                return format_string($value);
            });

        // Column description.
        $descriptionfieldsql = "{$mtcontabilita}.intro";
        if ($DB->get_dbfamily() === 'oracle') {
            $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
        }
        $columns[] = (new column(
            'description',
            new lang_string('description'),
            $this->get_entity_name()
        ))
            ->add_joins($this->get_joins())
            ->set_type(column::TYPE_LONGTEXT)
            ->add_field($descriptionfieldsql, 'intro')
            ->add_fields("{$mtcontabilita}.introformat, {$mtcontabilita}.id")
            ->set_is_sortable(false)
            ->add_callback(static function(?string $intro, stdClass $mtcontabilita): string {
                global $CFG;
                require_once("{$CFG->libdir}/filelib.php");

                if ($intro === null) {
                    return '';
                }

                [$course, $cm] = get_course_and_cm_from_instance($mtcontabilita->id, 'mtcontabilita');
                $context = context_module::instance($cm->id);

                $description = file_rewrite_pluginfile_urls($intro, 'pluginfile.php', $context->id, 'mod_mtcontabilita',
                    'intro', null);

                return format_text($description, $mtcontabilita->introformat, ['context' => $context]);
            });

        return $columns;
    }

    /**
     * get filters
     */
    protected function get_all_filters(): array {
        $mtcontabilita = $this->get_table_alias('mtcontabilita');

        // Filter name.
        $filters[] = (new filter(
            text::class,
            'name',
            new lang_string('name'),
            $this->get_entity_name(),
            "{$mtcontabilita}.name"
        ))
            ->add_joins($this->get_joins());

        return $filters;
    }
}
