<?php

declare(strict_types=1);

namespace App\Application\Admin\System;

use App\Application\Admin\Service\AdminLayoutProcessor;
use App\Application\Admin\System\Service\PhpInfoService;
use App\Framework\Attributes\Route;
use App\Framework\Auth\Auth;
use App\Framework\DateTime\Clock;
use App\Framework\Http\Method;
use App\Framework\Meta\MetaData;
use App\Framework\Router\Result\ViewResult;

final readonly class PhpInfoController
{
    public function __construct(
        private PhpInfoService $phpInfoService,
        private AdminLayoutProcessor $layoutProcessor,
        private Clock $clock,
    ) {
    }

    #[Auth]
    #[Route(path: '/admin/system/phpinfo', method: Method::GET, name: 'admin.system.phpinfo')]
    public function show(): ViewResult
    {
        $phpInfo = $this->phpInfoService->getStructuredInfo();

        // Prepare data for template
        $generalInfo = [];
        foreach ($phpInfo['general'] as $key => $value) {
            $generalInfo[] = [
                'key' => ucwords(str_replace('_', ' ', $key)),
                'value' => (string)$value,
            ];
        }

        $configInfo = [];
        foreach ($phpInfo['configuration'] as $key => $value) {
            $configInfo[] = [
                'key' => ucwords(str_replace('_', ' ', $key)),
                'value' => (string)$value,
            ];
        }

        $data = [
            'title' => 'PHP Information',
            'general_info' => $generalInfo,
            'config_info' => $configInfo,
            'extensions_count' => $phpInfo['extensions']['count'],
            'extensions_list' => $phpInfo['extensions']['list'],
            'current_year' => $this->clock->now()->format('Y'),
        ];

        $finalData = $this->layoutProcessor->processLayoutFromArray($data);

        return new ViewResult(
            template: 'phpinfo',
            metaData: new MetaData('PHP Information', 'PHP Information and Configuration'),
            data: $finalData
        );
    }
}
