<?php
// GET ?action=smm_services&api_key=KEY
// Returns all available SMM services

$api_markup = $GLOBALS['api_markup'];

try {
    $stmt = $pdo->query(
        "SELECT id, category, name, type, our_rate AS rate, min_quantity AS min, max_quantity AS max, refill, cancel
           FROM smm_services
          WHERE status = 1
          ORDER BY category ASC, name ASC"
    );
    $services = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    api_response(['status' => 'error', 'message' => 'Database error.'], 500);
}

$result = [];
foreach ($services as $svc) {
    $price = (float)$svc['rate'];
    if ($api_markup > 0) {
        $price = round($price * (1 + $api_markup / 100), 2);
    }
    $result[] = [
        'service_id' => (int)$svc['id'],
        'category'   => $svc['category'],
        'name'       => $svc['name'],
        'type'       => $svc['type'],
        'rate'       => number_format($price, 2, '.', ''),
        'min'        => (int)$svc['min'],
        'max'        => (int)$svc['max'],
        'refill'     => (bool)$svc['refill'],
        'cancel'     => (bool)$svc['cancel'],
    ];
}

api_response([
    'status'   => 'success',
    'count'    => count($result),
    'services' => $result,
]);
