<?php
require_once '../includes/auth.php';

$auth = new Auth();
$auth->requireLogin();

echo "<h1>內容管理API測試</h1>";

try {
    require_once '../config/database.php';
    $database = new Database();
    $db = $database->getConnection();
    
    // Test database connection
    echo "<h2>資料庫連接測試</h2>";
    echo "<p style='color: green;'>✅ 資料庫連接成功</p>";
    
    // Test content_plans table structure
    echo "<h2>content_plans表結構測試</h2>";
    $required_fields = ['id', 'brand_id', 'title', 'description', 'target_keywords', 'duration_months', 'frequency_per_week', 'status'];
    
    $query = "DESCRIBE content_plans";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $fields = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $existing_fields = array_column($fields, 'Field');
    
    foreach ($required_fields as $field) {
        if (in_array($field, $existing_fields)) {
            echo "<p style='color: green;'>✅ 欄位 '{$field}' 存在</p>";
        } else {
            echo "<p style='color: red;'>❌ 欄位 '{$field}' 不存在</p>";
        }
    }
    
    // Test content_articles table
    echo "<h2>content_articles表測試</h2>";
    $query = "SELECT COUNT(*) as count FROM content_articles";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo "<p style='color: green;'>✅ content_articles表存在，目前有 {$result['count']} 筆記錄</p>";
    
    // Test API call
    echo "<h2>API調用測試</h2>";
    echo "<button onclick='testAPI()'>測試內容計畫API</button>";
    echo "<div id='apiResult'></div>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ 發生錯誤: " . htmlspecialchars($e->getMessage()) . "</p>";
}
?>

<script>
async function testAPI() {
    const resultDiv = document.getElementById('apiResult');
    resultDiv.innerHTML = '<p>測試中...</p>';
    
    try {
        const response = await fetch('/api/content.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            credentials: 'same-origin',
            body: JSON.stringify({
                action: 'create_plan',
                brand_id: 1,
                title: '測試內容計畫',
                description: '這是一個測試內容計畫',
                target_keywords: '測試,關鍵字',
                duration_months: 3,
                frequency_per_week: 2
            })
        });
        
        const result = await response.json();
        
        if (result.success) {
            resultDiv.innerHTML = `
                <div style="background: #d4edda; padding: 15px; border-radius: 5px; margin: 10px 0;">
                    <h3 style="color: #155724; margin: 0 0 10px 0;">✅ API測試成功</h3>
                    <p><strong>計畫ID:</strong> ${result.data.plan_id}</p>
                    <p><strong>訊息:</strong> ${result.message}</p>
                </div>
            `;
        } else {
            resultDiv.innerHTML = `
                <div style="background: #f8d7da; padding: 15px; border-radius: 5px; margin: 10px 0;">
                    <h3 style="color: #721c24; margin: 0 0 10px 0;">❌ API測試失敗</h3>
                    <p><strong>錯誤訊息:</strong> ${result.message}</p>
                </div>
            `;
        }
    } catch (error) {
        resultDiv.innerHTML = `
            <div style="background: #f8d7da; padding: 15px; border-radius: 5px; margin: 10px 0;">
                <h3 style="color: #721c24; margin: 0 0 10px 0;">❌ 網路錯誤</h3>
                <p><strong>錯誤訊息:</strong> ${error.message}</p>
            </div>
        `;
    }
}
</script>
