<?php
// GET ?action=smm_status&api_key=KEY&order_id=ID
// Check SMM order status

$user_id  = $GLOBALS['api_user_id'];
$order_id = (int)($_GET['order_id'] ?? 0);

if (!$order_id) {
    api_response(['status' => 'error', 'message' => 'Missing order_id.'], 400);
}

try {
    $stmt = $pdo->prepare(
        "SELECT o.id, o.jap_order_id, o.link, o.quantity, o.charge, o.status, o.start_count, o.remains, o.created_at,
                s.name AS service_name, s.category
           FROM smm_orders o
           JOIN smm_services s ON s.id = o.smm_service_id
          WHERE o.id = :id AND o.user_id = :uid
          LIMIT 1"
    );
    $stmt->execute([':id' => $order_id, ':uid' => $user_id]);
    $order = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    api_response(['status' => 'error', 'message' => 'Database error.'], 500);
}

if (!$order) {
    api_response(['status' => 'error', 'message' => 'Order not found.'], 404);
}

api_response([
    'status'       => 'success',
    'order' => [
        'id'           => (int)$order['id'],
        'service'      => $order['service_name'],
        'category'     => $order['category'],
        'link'         => $order['link'],
        'quantity'     => (int)$order['quantity'],
        'charge'       => $order['charge'],
        'order_status' => $order['status'],
        'start_count'  => $order['start_count'],
        'remains'      => $order['remains'],
        'created_at'   => $order['created_at'],
    ],
]);
