<?php
if (PHP_SAPI !== 'cli') {
    @ini_set('display_startup_errors', '0');
    @ini_set('display_errors', '0');

    $requestUri = $_SERVER['REQUEST_URI'] ?? '';
    $isApiRequest = strpos($requestUri, '/api/') === 0 || strpos(parse_url($requestUri, PHP_URL_PATH) ?: '', '/api/') === 0;

    if ($isApiRequest) {
        ob_start(static function (string $buffer): string {
            return preg_replace(
                '#(?:\s*<br\s*/?>\s*)*<b>Notice</b>:\s*PHP Request Startup:\s*file created in the system\'s temporary directory\s*in\s*<b>Unknown</b>\s*on line\s*<b>0</b>\s*(?:<br\s*/?>\s*)*#i',
                '',
                $buffer
            ) ?? $buffer;
        });
    }
}
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

use think\App;

// [ 应用入口文件 ]

require __DIR__ . '/../vendor/autoload.php';

$stalePublicIndexHtml = __DIR__ . '/index.html';
if (PHP_SAPI !== 'cli' && is_file($stalePublicIndexHtml)) {
    @unlink($stalePublicIndexHtml);
}

if (!function_exists('shouldServeFrontendApp')) {
    function shouldServeFrontendApp(string $indexHtml): bool
    {
        if (PHP_SAPI === 'cli' || !is_file($indexHtml)) {
            return false;
        }

        $method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
        if (!in_array($method, ['GET', 'HEAD'], true)) {
            return false;
        }

        $requestPath = rawurldecode(parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/');
        $normalizedPath = '/' . ltrim($requestPath, '/');

        if (strpos($normalizedPath, '/api/') === 0 || $normalizedPath === '/api') {
            return false;
        }

        $passthroughPaths = [
            '/debug/docking',
            '/debug/fix',
            '/notification-test',
        ];
        if (in_array($normalizedPath, $passthroughPaths, true)) {
            return false;
        }

        $extension = pathinfo($normalizedPath, PATHINFO_EXTENSION);
        if ($extension !== '' && strtolower($extension) !== 'html') {
            return false;
        }

        $accept = strtolower($_SERVER['HTTP_ACCEPT'] ?? '');
        if ($accept !== '' && strpos($accept, 'text/html') === false && strpos($accept, '*/*') === false) {
            return false;
        }

        return true;
    }
}

if (!function_exists('escapeHtmlContent')) {
    function escapeHtmlContent(string $value): string
    {
        return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    }
}

if (!function_exists('upsertMetaTag')) {
    function upsertMetaTag(string $html, string $name, string $content): string
    {
        $escapedContent = escapeHtmlContent($content);
        $pattern = '/<meta\s+name=["\']' . preg_quote($name, '/') . '["\']\s+content=["\'][^"\']*["\']\s*\/?>/i';
        $replacement = '<meta name="' . $name . '" content="' . $escapedContent . '" />';

        if (preg_match($pattern, $html) === 1) {
            return preg_replace($pattern, $replacement, $html, 1) ?? $html;
        }

        return preg_replace('/<\/head>/i', "    {$replacement}\n  </head>", $html, 1) ?? $html;
    }
}

if (!function_exists('replaceFaviconLink')) {
    function replaceFaviconLink(string $html, string $iconUrl): string
    {
        if ($iconUrl === '') {
            return $html;
        }

        $escapedUrl = escapeHtmlContent($iconUrl);
        $shortcutIcon = '<link rel="shortcut icon" type="image/x-icon" href="' . $escapedUrl . '" />';
        $icon = '<link rel="icon" href="' . $escapedUrl . '" />';

        $html = preg_replace('/<link\s+[^>]*rel=["\']shortcut icon["\'][^>]*>/i', $shortcutIcon, $html, 1) ?? $html;
        if (preg_match('/<link\s+[^>]*rel=["\']icon["\'][^>]*>/i', $html) === 1) {
            $html = preg_replace('/<link\s+[^>]*rel=["\']icon["\'][^>]*>/i', $icon, $html, 1) ?? $html;
            return $html;
        }

        return preg_replace('/<\/head>/i', "    {$shortcutIcon}\n    {$icon}\n  </head>", $html, 1) ?? $html;
    }
}

if (!function_exists('resolveAssetUrl')) {
    function resolveAssetUrl(string $path): string
    {
        $path = trim($path);
        if ($path === '') {
            return '';
        }

        if (preg_match('/^(https?:)?\/\//i', $path) === 1 || str_starts_with($path, 'data:')) {
            return $path;
        }

        return '/' . ltrim($path, '/');
    }
}

if (!function_exists('getFrontendSeoConfig')) {
    function getFrontendSeoConfig(): array
    {
        $seo = [
            'title' => 'Entropy',
            'description' => '',
            'keywords' => '',
            'icon' => '',
        ];

        try {
            $settings = \app\service\SettingService::getMany([
                'site_name',
                'site_description',
                'site_keywords',
                'site_icon',
            ]);

            $seo['title'] = trim((string)($settings['site_name'] ?? $seo['title'])) ?: 'Entropy';
            $seo['description'] = trim((string)($settings['site_description'] ?? ''));
            $seo['keywords'] = trim((string)($settings['site_keywords'] ?? ''));
            $seo['icon'] = resolveAssetUrl((string)($settings['site_icon'] ?? ''));
        } catch (\Throwable $exception) {
            error_log('Frontend SEO config load failed: ' . $exception->getMessage());
        }

        return $seo;
    }
}

if (!function_exists('injectFrontendSeo')) {
    function injectFrontendSeo(string $html): string
    {
        $seo = getFrontendSeoConfig();
        $title = escapeHtmlContent($seo['title']);

        if (preg_match('/<title>.*?<\/title>/is', $html) === 1) {
            $html = preg_replace('/<title>.*?<\/title>/is', "<title>{$title}</title>", $html, 1) ?? $html;
        } else {
            $html = preg_replace('/<\/head>/i', "    <title>{$title}</title>\n  </head>", $html, 1) ?? $html;
        }

        if ($seo['description'] !== '') {
            $html = upsertMetaTag($html, 'description', $seo['description']);
        }

        if ($seo['keywords'] !== '') {
            $html = upsertMetaTag($html, 'keywords', $seo['keywords']);
        }

        if ($seo['icon'] !== '') {
            $html = replaceFaviconLink($html, $seo['icon']);
        }

        return $html;
    }
}

if (!function_exists('serveFrontendApp')) {
    function serveFrontendApp(string $indexHtml): void
    {
        $content = file_get_contents($indexHtml);
        if ($content === false) {
            return;
        }

        $content = injectFrontendSeo($content);

        header('Content-Type: text/html; charset=utf-8');
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'HEAD') {
            return;
        }

        echo $content;
    }
}

$app = new App();
$frontendIndexFile = __DIR__ . '/index.html';
if (shouldServeFrontendApp($frontendIndexFile)) {
    serveFrontendApp($frontendIndexFile);
    return;
}

if (PHP_SAPI !== 'cli' && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && !headers_sent()) {
    ini_set('zlib.output_compression', 'On');
    ini_set('zlib.output_compression_level', '5');
    header('Vary: Accept-Encoding');
}

// 执行HTTP应用并响应
$http = $app->http;

$response = $http->run();

$response->send();

$http->end($response);
