<?php
session_start();
header('Content-Type: application/json; charset=utf-8');

if (!isset($_SESSION['user_id'])) {
    echo json_encode([
        'success' => false,
        'message' => 'Unauthorized'
    ]);
    exit;
}

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

if (!isset($connection) || $connection->connect_error) {
    echo json_encode([
        'success' => false,
        'message' => 'Database connection failed'
    ]);
    exit;
}

$services = [];
$stmt = $connection->prepare("SELECT id, title FROM services WHERE is_active = 1 ORDER BY sort_order ASC, id ASC");
if ($stmt) {
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        $title = trim((string)($row['title'] ?? ''));
        if ($title === '') {
            continue;
        }
        $services[] = [
            'id' => (int)($row['id'] ?? 0),
            'title' => $title
        ];
    }
    $stmt->close();
}

echo json_encode([
    'success' => true,
    'services' => $services
]);

