<?php
session_start();
require_once '../includes/auth.php';
require_once '../config/database.php';

echo "<h1>SEO分析調試</h1>";

// 檢查認證
$auth = new Auth();
echo "<h2>認證狀態</h2>";
echo "登入狀態: " . ($auth->isLoggedIn() ? "已登入" : "未登入") . "<br>";
echo "用戶ID: " . ($_SESSION['user_id'] ?? '未設置') . "<br>";
echo "會話ID: " . session_id() . "<br>";

// 檢查資料庫連接
echo "<h2>資料庫連接</h2>";
$database = new Database();
$db = $database->getConnection();
if ($db) {
    echo "資料庫連接: 成功<br>";
    
    // 檢查品牌資料
    $query = "SELECT * FROM brands LIMIT 3";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $brands = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "品牌數量: " . count($brands) . "<br>";
    echo "<h3>品牌列表:</h3>";
    foreach ($brands as $brand) {
        echo "ID: " . $brand['id'] . ", 名稱: " . $brand['name'] . "<br>";
    }
} else {
    echo "資料庫連接: 失敗<br>";
}

// 測試API調用
echo "<h2>API測試</h2>";
if (count($brands) > 0) {
    $test_brand_id = $brands[0]['id'];
    echo "測試品牌ID: " . $test_brand_id . "<br>";
    
    // 模擬API調用
    $_POST = [
        'brand_id' => $test_brand_id,
        'analysis_type' => 'basic'
    ];
    
    echo "<h3>模擬API調用結果:</h3>";
    ob_start();
    include '../api/seo_analysis.php';
    $api_result = ob_get_clean();
    echo "<pre>" . htmlspecialchars($api_result) . "</pre>";
}
?>
