<?php

declare(strict_types=1);

use App\Core\Middleware\AuthMiddleware;
use App\Core\Routing\RedirectAction;
use App\Modules\Admin\Presentation\Controllers\DashboardController;
use App\Modules\Reporting\Presentation\Controllers\AlertsDashboardController;
use App\Controllers\LicenseConsoleController;

$dashboardController = new DashboardController();
$alertsController = new AlertsDashboardController();
$licenseConsoleController = new LicenseConsoleController();

$router->get('/', [$dashboardController, 'index'], [new AuthMiddleware()]);
$router->get('/index.php', new RedirectAction('/admin/dashboard'), [new AuthMiddleware()]);
$router->get('/dashboard', [$dashboardController, 'index'], [new AuthMiddleware()]);
$router->get('/admin/dashboard', [$dashboardController, 'index'], [new AuthMiddleware()]);

$router->get('/health', static function (): void {
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode(['status' => 'ok', 'app' => 'storeapp'], JSON_UNESCAPED_UNICODE);
});

$router->get('/system/schema-status', static function (): void {
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode([
        'runtime_schema_patches' => \App\Core\SchemaRuntime::runtimePatchesAllowed(),
        'requested' => \App\Core\SchemaRuntime::statusReport(),
        'manifest' => require APP_BASE_PATH . '/database/migrations/manifest.php',
    ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}, [new AuthMiddleware()]);

$router->get('/system/license-status', static function (): void {
    $config = require APP_PATH . '/config.php';
    $manager = new \App\Services\License\LicenseManager($config);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode([
        'state' => license_state(),
        'fingerprint' => \App\Services\License\ServerFingerprint::value($config),
        'license_path' => APP_BASE_PATH . '/storage/license/license.json',
        'dev_override_path' => APP_BASE_PATH . '/storage/license/dev-override.json',
        'public_key_path' => APP_BASE_PATH . '/storage/license/public.key',
        'template' => $manager->activationTemplate(),
        'activation_request_template' => $manager->activationRequestTemplate(),
        'developer_console_enabled' => $manager->isDeveloperConsoleEnabled(),
        'dev_override_template' => $manager->devOverrideTemplate(),
        'signing_payload' => $manager->signingPayloadTemplate(),
    ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
});


$router->get('/system/license-request', [$licenseConsoleController, 'exportRequest'], [new AuthMiddleware()]);
$router->get('/system/license-admin', [$licenseConsoleController, 'index'], [new AuthMiddleware()]);
$router->post('/system/license-admin/issue', [$licenseConsoleController, 'issue'], [new AuthMiddleware()]);
$router->post('/system/license-admin/keypair', [$licenseConsoleController, 'generateKeypair'], [new AuthMiddleware()]);
$router->get('/system/license-admin/download', [$licenseConsoleController, 'download'], [new AuthMiddleware()]);

$router->get('/admin', new RedirectAction('/admin/dashboard'), [new AuthMiddleware()]);
$router->get('/inventory', new RedirectAction('/inventory/items'), [new AuthMiddleware()]);
$router->get('/finance', new RedirectAction('/finance/cashboxes'), [new AuthMiddleware()]);
$router->get('/purchases', new RedirectAction('/purchases/invoices'), [new AuthMiddleware()]);
$router->get('/parties/customers-suppliers', new RedirectAction('/parties'), [new AuthMiddleware()]);
// Reports hub is registered in routes/web/reporting.php
$router->get('/alerts', [$alertsController, 'index'], [new AuthMiddleware()]);
