<?php
/**
 * Testimonials API Endpoint
 */

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

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

try {
    $pdo = getDBConnection();
    
    $stmt = $pdo->query("
        SELECT id, client_name, client_position, company_name, testimonial_text, 
               rating, service_type, client_photo, featured
        FROM testimonials
        WHERE status = 'active'
        ORDER BY display_order ASC, created_at DESC
    ");
    
    $testimonials = [];
    while ($row = $stmt->fetch()) {
        $testimonials[] = [
            'id' => $row['id'],
            'client_name' => $row['client_name'],
            'client_position' => $row['client_position'],
            'company_name' => $row['company_name'],
            'testimonial' => $row['testimonial_text'],
            'rating' => intval($row['rating']),
            'service_type' => $row['service_type'],
            'photo' => $row['client_photo'] ? '/backend/uploads/testimonials/' . $row['client_photo'] : null,
            'featured' => (bool)$row['featured']
        ];
    }
    
    echo json_encode([
        'success' => true,
        'testimonials' => $testimonials
    ]);
    
} catch (Exception $e) {
    error_log("Testimonials API error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'testimonials' => []
    ]);
}

