<?php

require_once APP_ROOT . '/src/Database.php';
require_once APP_ROOT . '/src/Parsedown.php';
require_once APP_ROOT . '/src/ErrorHandler.php';

class LicenceController
{
    public static function create(): self
    {
        return new self();
    }

    public function handle(): array
    {
        try {
            $db = Database::getInstance();
            $dbPage = $db->getPage('licenses');
            $content = $dbPage ? $dbPage['content'] : '';
            $pageTitle = $dbPage ? $dbPage['title'] : 'Licences';
        } catch (Exception $e) {
            ErrorHandler::log('licence_page', $e);
            $content = '';
            $pageTitle = 'Licences';
        }

        $pd = new Parsedown();
        $pd->setSafeMode(true);
        $html = $pd->text($content);

        return [
            'content'          => $content,
            'html'             => $html,
            'pageTitle'        => $pageTitle . ' – XAMXAM',
            'metaDescription'  => "Informations sur les licences d'utilisation des mémoires publiés sur XAMXAM, le répertoire des TFE de l'erg.",
            'currentNav'       => 'licence',
            'extraCss'         => ['/assets/css/apropos.css'],
            'bodyClass'        => 'apropos-body',
        ];
    }
}
