<?php
/**
 * Services 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, title, short_description, full_description, icon_class, icon_type, 
               color, category, features, image_url, display_order
        FROM services 
        WHERE status = 'active'
        ORDER BY display_order ASC, created_at DESC
    ");
    
    $services = [];
    while ($row = $stmt->fetch()) {
        $features = json_decode($row['features'], true);
        if (!is_array($features)) {
            $features = [];
        }
        
        $services[] = [
            'id' => (int)$row['id'],
            'title' => $row['title'],
            'short_description' => $row['short_description'],
            'full_description' => $row['full_description'],
            'icon_class' => $row['icon_class'],
            'icon_type' => $row['icon_type'],
            'color' => $row['color'],
            'category' => $row['category'],
            'features' => $features,
            'image_url' => $row['image_url'] ? '/backend/uploads/services/' . $row['image_url'] : null,
            'display_order' => (int)$row['display_order']
        ];
    }
    
    echo json_encode([
        'success' => true,
        'services' => $services
    ]);
    
} catch (Exception $e) {
    error_log("Services API error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'services' => []
    ]);
}

