<?php
// GET ?action=sms_services&api_key=KEY&country_id=ID
// Returns available services for a country

$country_id = trim($_GET['country_id'] ?? '');
if (empty($country_id)) {
    api_response(['status' => 'error', 'message' => 'Missing country_id parameter.'], 400);
}

$api_markup = $GLOBALS['api_markup'];

try {
    $stmt = $pdo->prepare(
        "SELECT s.service_id, s.service_name, s.service_price
           FROM service s
          WHERE s.server_id = :sid AND s.status = '1'
          ORDER BY s.service_name ASC"
    );
    $stmt->execute([':sid' => $country_id]);
    $services = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    api_response(['status' => 'error', 'message' => 'Database error.'], 500);
}

// Apply API markup
$result = [];
foreach ($services as $svc) {
    $price = (float)$svc['service_price'];
    if ($api_markup > 0) {
        $price = round($price * (1 + $api_markup / 100), 2);
    }
    $result[] = [
        'service_id'   => $svc['service_id'],
        'service_name' => $svc['service_name'],
        'price'        => number_format($price, 2, '.', ''),
    ];
}

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