<?php

class ChainPay_Client {
    
    private $api_url;
    private $license_key;
    private $domain;
    
    public function __construct($api_url, $license_key = null, $domain = null) {
        $this->api_url = rtrim($api_url, '/');
        $this->license_key = $license_key;
        $this->domain = $domain ?: $this->get_current_domain();
    }
    
    private function get_current_domain() {
        $protocol = is_ssl() ? 'https://' : 'http://';
        return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
    }
    
    private function make_request($endpoint, $data = array()) {
        $data['license_key'] = $this->license_key;
        $data['domain'] = $this->domain;
        
        $args = array(
            'body' => $data,
            'timeout' => 30,
            'headers' => array(
                'Content-Type' => 'application/x-www-form-urlencoded'
            )
        );
        
        $response = wp_remote_post($this->api_url . '/wp-json/chainpay-license/v1/' . $endpoint, $args);
        
        if (is_wp_error($response)) {
            return array(
                'success' => false,
                'error' => $response->get_error_message()
            );
        }
        
        $body = wp_remote_retrieve_body($response);
        $result = json_decode($body, true);
        
        return $result;
    }
    
    public function validate() {
        $result = $this->make_request('validate');
        return $result;
    }
    
    public function activate($email = '') {
        $data = array();
        if ($email) {
            $data['email'] = $email;
        }
        return $this->make_request('activate', $data);
    }
    
    public function deactivate() {
        return $this->make_request('deactivate');
    }
    
    public function track_usage($feature, $count = 1, $period = 'daily') {
        $result = wp_remote_post($this->api_url . '/wp-json/chainpay-license/v1/usage', array(
            'body' => array(
                'license_key' => $this->license_key,
                'domain' => $this->domain,
                'feature' => $feature,
                'count' => $count,
                'period' => $period
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($result)) {
            return array('success' => false, 'error' => $result->get_error_message());
        }
        
        return json_decode(wp_remote_retrieve_body($result), true);
    }
    
    public function check_limit($feature) {
        $result = wp_remote_post($this->api_url . '/wp-json/chainpay-license/v1/usage/check', array(
            'body' => array(
                'license_key' => $this->license_key,
                'domain' => $this->domain,
                'feature' => $feature
            ),
            'timeout' => 30
        ));
        
        if (is_wp_error($result)) {
            return array('success' => false, 'error' => $result->get_error_message());
        }
        
        return json_decode(wp_remote_retrieve_body($result), true);
    }
    
    public function is_valid() {
        $result = $this->validate();
        return isset($result['success']) && $result['success'] && isset($result['valid']) && $result['valid'];
    }
}
