<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Session;
use App\Services\MailTemplateService;
use Throwable;

final class MailTemplateController extends Controller
{
    public function index(): void
    {
        $this->view('parametres/mail_templates', [
            'title' => 'Modèles d’e-mails',
            'templates' => (new MailTemplateService())->all(),
        ]);
    }

    public function save(): void
    {
        try {
            (new MailTemplateService())->save(Request::all());
            Session::flash('success', 'Modèle d’e-mail enregistré.');
        } catch (Throwable $exception) {
            Session::flash('error', $exception->getMessage());
        }

        $this->redirect('parametres_mail_templates');
    }
}
