<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Models\DemoData;
use App\Services\DocumentGenerator;

final class DocumentController extends Controller
{
    public function index(): string
    {
        return $this->view('documents/index', [
            'title' => 'Генератори',
            'templates' => DemoData::documentTemplates(),
        ]);
    }

    public function preview(): string
    {
        if ($response = $this->ensureCsrf()) {
            return $response;
        }

        $document = (new DocumentGenerator())->generate((string) ($this->input()['type'] ?? 'signal'), $this->input());

        return $this->json(['ok' => true, 'document' => $document]);
    }
}
