<?php
/**
 * FAQs API Endpoint
 */

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

require_once __DIR__ . '/../config/database.php';

try {
    $pdo = getDBConnection();
    
    $category = $_GET['category'] ?? null;
    
    $query = "SELECT id, question, answer, category FROM faqs WHERE status = 'active'";
    if ($category) {
        $query .= " AND category = :category";
    }
    $query .= " ORDER BY display_order ASC, created_at DESC";
    
    $stmt = $pdo->prepare($query);
    if ($category) {
        $stmt->execute([':category' => $category]);
    } else {
        $stmt->execute();
    }
    
    $faqs = [];
    while ($row = $stmt->fetch()) {
        $faqs[] = [
            'id' => $row['id'],
            'question' => $row['question'],
            'answer' => $row['answer'],
            'category' => $row['category']
        ];
    }
    
    echo json_encode([
        'success' => true,
        'faqs' => $faqs
    ]);
    
} catch (Exception $e) {
    error_log("FAQs API error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'faqs' => []
    ]);
}

