<?php
require_once '../../config/db.php';

header('Content-Type: application/json');

try {
    $category = isset($_GET['category']) ? $_GET['category'] : null;
    $seller_id = isset($_GET['seller_id']) ? (int)$_GET['seller_id'] : null;

    $sql = "SELECT s.*, u.username, u.avatar 
            FROM services s 
            JOIN users u ON s.seller_id = u.id 
            WHERE s.is_active = TRUE";
    
    $params = [];

    if ($category) {
        $sql .= " AND s.category = ?";
        $params[] = $category;
    }

    if ($seller_id) {
        $sql .= " AND s.seller_id = ?";
        $params[] = $seller_id;
    }

    $sql .= " ORDER BY s.created_at DESC LIMIT 50";

    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    $services = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'count' => count($services),
        'data' => $services
    ]);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
?>
