<?php

declare(strict_types=1);

namespace App\Application\Actions\Admin;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Twig\Environment;
use PDO;

class AccessLogsAction
{
    private PDO $pdo;
    private Environment $twig;

    public function __construct(PDO $pdo, Environment $twig)
    {
        $this->pdo = $pdo;
        $this->twig = $twig;
    }

    public function __invoke(Request $request, Response $response, array $args): Response
    {
        $stmt = $this->pdo->query('SELECT * FROM access_logs ORDER BY created_at DESC LIMIT 20');
        $logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $html = $this->twig->render('admin/access_logs.twig', ['logs' => $logs]);
        $response->getBody()->write($html);
        return $response->withHeader('Content-Type', 'text/html');
    }
}
