<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Models\LicenseClassTemplate;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class TemplatesController
{
    public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $tenantId = $request->getAttribute('tenant_id');
        $templates = LicenseClassTemplate::where('tenant_id', $tenantId)
            ->with('requirements')
            ->get();
        return $this->json($response, $templates->toArray());
    }

    private function json(ResponseInterface $response, array $data, int $status = 200): ResponseInterface
    {
        $response->getBody()->write(json_encode($data));
        return $response->withStatus($status)->withHeader('Content-Type', 'application/json');
    }
}