<?php
/**
 * 短链接跳转入口
 */

require_once __DIR__ . '/core/Shortener.php';
require_once __DIR__ . '/core/Utils.php';

// 获取URL路径中的订单号
$path = $_SERVER['REQUEST_URI'];
$path = trim($path, '/');

// 移除查询参数
if (strpos($path, '?') !== false) {
    $path = substr($path, 0, strpos($path, '?'));
}

$orderNo = $path;

// 验证订单号格式（支持24-26位）
if (empty($orderNo) || !preg_match('/^[a-zA-Z0-9]{24,26}$/', $orderNo)) {
    // 订单号格式不正确，显示错误页面
    header('HTTP/1.1 404 Not Found');
    echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>404 Not Found</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
        }
        h1 {
            color: #333;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <h1>404 Not Found</h1>
    <p>您访问的短链接不存在或已过期</p>
</body>
</html>';
    exit;
}

// 引入Database类
require_once __DIR__ . '/core/Database.php';

// 初始化短链接生成器
$shortener = new Shortener();

// 获取配置的超时时间
$config = require __DIR__ . '/config/config.php';
$expireHours = $config['shortener']['expire_hours'] ?? 1;

try {
    // 通过订单号获取原始数据
    $originalData = $shortener->getOriginalData($orderNo);
    
    if ($originalData) {
        // 跳转到1.php页面，传递订单号参数
        header('Location: /1.php?order_no=' . urlencode($orderNo));
        exit;
    } else {
        // 检查短链接是否存在（用于区分不存在和超时）
        $db = new Database();
        $linkExists = $db->getOne("SELECT id FROM short_links WHERE order_no = ?", [$orderNo]);
        
        if ($linkExists) {
            // 短链接存在但已超时
            header('HTTP/1.1 410 Gone');
            echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>链接已超时</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
            background-color: #f8f9fa;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #ff4d4f;
            font-size: 24px;
            margin-bottom: 20px;
        }
        p {
            color: #666;
            font-size: 16px;
            line-height: 1.6;
        }
        .tip {
            margin-top: 20px;
            color: #1890ff;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>链接已超时</h1>
        <p>您访问的链接已超过有效期（{$expireHours}小时），无法继续访问。</p>
        <p class="tip">提示：短链接的有效期为{$expireHours}小时，请在有效期内使用。</p>
    </div>
</body>
</html>';
        } else {
            // 短链接不存在
            header('HTTP/1.1 404 Not Found');
            echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>404 Not Found</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
        }
        h1 {
            color: #333;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <h1>404 Not Found</h1>
    <p>您访问的短链接不存在或已过期</p>
</body>
</html>';
        }
        exit;
    }
} catch (Exception $e) {
    // 记录错误日志
    Utils::logError('Short Link Redirect Error: ' . $e->getMessage());
    
    // 显示错误页面
    header('HTTP/1.1 500 Internal Server Error');
    echo '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Internal Server Error</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            padding: 50px;
        }
        h1 {
            color: #333;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <h1>Internal Server Error</h1>
    <p>服务器内部错误，请稍后重试</p>
</body>
</html>';
    exit;
}
?>
