<?php
/// file: app/Core/Controller.php

declare(strict_types=1);

namespace App\Core;

abstract class Controller
{
    public function __construct(protected readonly App $app)
    {
    }

    /** @param array<string, mixed> $data */
    protected function view(string $template, array $data = [], ?string $layout = 'layouts/base'): string
    {
        return View::render($template, $data, $layout, $this->app);
    }

    protected function adminView(string $template, array $data = []): string
    {
        return View::render($template, $data, 'layouts/admin', $this->app);
    }

    /** Admin layout without sidebar (e.g. login). */
    protected function adminGuestView(string $template, array $data = []): string
    {
        return View::render($template, $data, 'layouts/admin-guest', $this->app);
    }

    /** @param array<string, mixed> $payload */
    protected function json(array $payload, int $status = 200): Response
    {
        return Response::json($payload, $status);
    }

    protected function redirect(string $url, int $code = 302): Response
    {
        return Redirect::to($url, $code);
    }
}
