<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Repositories\PetitionRepository;
use App\Services\DocumentGenerator;

final class PetitionController extends Controller
{
    public function index(): string
    {
        return $this->view('petitions/index', [
            'title' => 'Петиции',
            'petitions' => (new PetitionRepository())->active(),
        ]);
    }

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

        $petition = (new PetitionRepository())->create($this->input());
        $document = (new DocumentGenerator())->generate('petition', $this->input());

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