<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech

/**
 * JAP (JustAnotherPanel) SMM API Client
 * Compatible with ALL standard SMM panels (Peakerr, PerfectPanel, etc.)
 * Just change $api_url and $api_key to switch providers.
 *
 * API Endpoint: https://justanotherpanel.com/api/v2
 * All requests are POST with key + action params.
 */
class JAP {

    private $api_url;
    private $api_key;

    public function __construct($api_key, $api_url = 'https://justanotherpanel.com/api/v2') {
        $this->api_key  = $api_key;
        $this->api_url  = rtrim($api_url, '/');
    }

    // ── Core HTTP POST ───────────────────────────────────────────
    private function call(array $params) {
        $params['key'] = $this->api_key;

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL            => $this->api_url,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => http_build_query($params),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_HTTPHEADER     => ['Content-Type: application/x-www-form-urlencoded'],
        ]);

        $response   = curl_exec($curl);
        $curl_error = curl_error($curl);
        $http_code  = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        if ($curl_error) {
            return ['success' => false, 'error' => 'cURL error: ' . $curl_error];
        }

        $decoded = json_decode($response, true);
        if ($decoded === null) {
            return ['success' => false, 'error' => 'Invalid response from API', 'raw' => $response];
        }

        // API-level errors come back as {"error": "message"}
        if (isset($decoded['error'])) {
            return ['success' => false, 'error' => $decoded['error'], 'raw' => $decoded];
        }

        return ['success' => true, 'data' => $decoded, 'code' => $http_code];
    }

    // ════════════════════════════════════════════════
    //  SERVICES
    // ════════════════════════════════════════════════

    /**
     * Get all available services from JAP
     * Returns array of services with id, name, type, category, rate, min, max, refill, cancel
     */
    public function getServices() {
        return $this->call(['action' => 'services']);
    }

    // ════════════════════════════════════════════════
    //  ORDERS
    // ════════════════════════════════════════════════

    /**
     * Place a new order
     * @param int    $service   JAP service ID
     * @param string $link      Target social media link
     * @param int    $quantity  Number of followers/likes/views etc.
     * @param array  $extras    Optional: ['runs'=>1, 'interval'=>30] for drip-feed
     *                                    ['comments'=>"comment1\ncomment2"] for custom comments
     */
    public function addOrder($service, $link, $quantity, $extras = []) {
        $params = array_merge([
            'action'   => 'add',
            'service'  => $service,
            'link'     => $link,
            'quantity' => $quantity,
        ], $extras);

        return $this->call($params);
    }

    /**
     * Get status of a single order
     * Returns: charge, start_count, status, remains, currency
     */
    public function getOrderStatus($order_id) {
        return $this->call(['action' => 'status', 'order' => $order_id]);
    }

    /**
     * Get status of multiple orders at once (up to 100)
     * @param array $order_ids  Array of order IDs [1, 2, 3]
     */
    public function getMultipleOrderStatus($order_ids) {
        return $this->call(['action' => 'status', 'orders' => implode(',', $order_ids)]);
    }

    // ════════════════════════════════════════════════
    //  REFILLS
    // ════════════════════════════════════════════════

    /**
     * Request a refill for an order (only for refill-eligible services)
     */
    public function requestRefill($order_id) {
        return $this->call(['action' => 'refill', 'order' => $order_id]);
    }

    /**
     * Request refills for multiple orders
     * @param array $order_ids  [1, 2, 3]
     */
    public function requestMultipleRefills($order_ids) {
        return $this->call(['action' => 'refill', 'orders' => implode(',', $order_ids)]);
    }

    /**
     * Get refill status
     */
    public function getRefillStatus($refill_id) {
        return $this->call(['action' => 'refill_status', 'refill' => $refill_id]);
    }

    // ════════════════════════════════════════════════
    //  CANCEL
    // ════════════════════════════════════════════════

    /**
     * Cancel one or multiple orders (only works for cancel-eligible services)
     * @param array $order_ids  [1, 2, 3]
     */
    public function cancelOrders($order_ids) {
        return $this->call(['action' => 'cancel', 'orders' => implode(',', $order_ids)]);
    }

    // ════════════════════════════════════════════════
    //  BALANCE
    // ════════════════════════════════════════════════

    /**
     * Get your JAP account balance
     * Returns: balance (USD), currency
     */
    public function getBalance() {
        return $this->call(['action' => 'balance']);
    }
}
?>