<?php
/**
 * SMS-Man API Wrapper
 * Docs: https://sms-man.com/api
 * Base URL: https://api.sms-man.com/control/
 * All requests: token param required
 */
class SMSMan {

    private $token;
    private $base_url;

    public function __construct($token, $base_url = 'https://api.sms-man.com/control') {
        $this->token    = $token;
        $this->base_url = rtrim($base_url, '/');
    }

    // ── Core HTTP request ────────────────────────────────────────
    private function request($endpoint, $params = []) {
        $params['token'] = $this->token;
        $url = $this->base_url . '/' . ltrim($endpoint, '/') . '?' . http_build_query($params);

        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL            => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT        => 20,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_USERAGENT      => 'SMSMan-PHP/1.0',
        ]);
        $raw  = curl_exec($ch);
        $err  = curl_error($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($err) return ['success' => false, 'error_code' => 'curl_error', 'error_msg' => $err];
        if ($raw === false) return ['success' => false, 'error_code' => 'empty_response', 'error_msg' => 'Empty response'];

        $data = json_decode($raw, true);
        if ($data === null) return ['success' => false, 'error_code' => 'invalid_json', 'error_msg' => $raw];

        // SMS-Man returns success:false on errors
        if (isset($data['success']) && $data['success'] === false) {
            return ['success' => false, 'error_code' => $data['error_code'] ?? 'unknown', 'error_msg' => $data['error_msg'] ?? 'Unknown error'];
        }

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

    // ── 1. Get balance ───────────────────────────────────────────
    // Returns: {"balance":"12.34"}
    public function getBalance() {
        return $this->request('get-balance');
    }

    // ── 2. Get all countries ─────────────────────────────────────
    // Returns: [{"id":0,"title":"Russia"}, ...]
    public function getCountries() {
        return $this->request('countries');
    }

    // ── 3. Get all applications/services ─────────────────────────
    // Returns: [{"id":"1","name":"Telegram","code":"tg"}, ...]
    public function getApplications() {
        return $this->request('applications');
    }

    // ── 4. Get limits (available numbers per country+service) ────
    // Returns: [{"application_id":"1","country_id":"1","numbers":"32302"}, ...]
    public function getLimits($country_id = null, $application_id = null) {
        $params = [];
        if ($country_id !== null)     $params['country_id']     = $country_id;
        if ($application_id !== null) $params['application_id'] = $application_id;
        return $this->request('limits', $params);
    }

    // ── 5. Get prices ────────────────────────────────────────────
    // Returns: {"country_id":{"app_id":{"cost":"15","count":6455}}}
    public function getPrices($country_id = null) {
        $params = [];
        if ($country_id !== null) $params['country_id'] = $country_id;
        return $this->request('get-prices', $params);
    }

    // ── 6. Request / buy a number ────────────────────────────────
    // Returns: {"request_id":123,"number":"79001234567","country_id":1,"application_id":1}
    public function getNumber($country_id, $application_id) {
        return $this->request('get-number', [
            'country_id'     => $country_id,
            'application_id' => $application_id,
        ]);
    }

    // ── 7. Get SMS for a request ─────────────────────────────────
    // Returns when SMS arrived: {"request_id":1,"sms_code":"12345","sms_full_msg":"Your code: 12345","number":"79001234567"}
    // Returns while waiting:    {"request_id":1,"error_code":"wait_sms","error_msg":"Still waiting...","number":"79001234567"}
    public function getSMS($request_id) {
        return $this->request('get-sms', ['request_id' => $request_id]);
    }

    // ── 8. Set status ────────────────────────────────────────────
    // status: "cancel" = cancel & refund, "finish" = mark complete
    // Returns: {"request_id":1,"success":true}
    public function setStatus($request_id, $status = 'cancel') {
        return $this->request('set-status', [
            'request_id' => $request_id,
            'status'     => $status,
        ]);
    }
}