<?php
/**
 * Diinta License Hub — WordPress Client SDK
 * Drop this file into your WordPress plugin: include_once __DIR__ . '/diinta-wp-client.php';
 */

if (!defined('ABSPATH')) {
    exit;
}

class Diinta_WP_License_Client {
    protected $api_url;
    protected $product_code;
    protected $option_key;

    public function __construct($product_code, $api_url = 'https://diinta.so/api/v1/public') {
        $this->product_code = $product_code;
        $this->api_url = rtrim($api_url, '/');
        $this->option_key = 'diinta_license_' . sanitize_key($product_code);
    }

    public function activate($license_key) {
        $instance_id = $this->get_instance_id();
        $domain = parse_url(home_url(), PHP_URL_HOST);

        $response = wp_remote_post($this->api_url . '/licenses/activate', [
            'timeout' => 15,
            'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
            'body'    => json_encode([
                'product_code' => $this->product_code,
                'license_key'  => trim($license_key),
                'domain'       => $domain,
                'instance_id'  => $instance_id,
            ]),
        ]);

        if (is_wp_error($response)) {
            return ['ok' => false, 'message' => $response->get_error_message()];
        }

        $body = json_decode(wp_remote_retrieve_body($response), true);
        if (!empty($body['ok'])) {
            update_option($this->option_key . '_key', trim($license_key));
            update_option($this->option_key . '_token', $body['token'] ?? '');
            set_transient($this->option_key . '_verified', true, DAY_IN_SECONDS);
            return ['ok' => true, 'data' => $body['data'] ?? []];
        }

        return ['ok' => false, 'message' => $body['message'] ?? 'Activation failed'];
    }

    public function is_valid() {
        if (get_transient($this->option_key . '_verified')) {
            return true;
        }

        $license_key = get_option($this->option_key . '_key');
        if (empty($license_key)) {
            return false;
        }

        $instance_id = $this->get_instance_id();
        $domain = parse_url(home_url(), PHP_URL_HOST);

        $response = wp_remote_post($this->api_url . '/licenses/verify', [
            'timeout' => 10,
            'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
            'body'    => json_encode([
                'product_code' => $this->product_code,
                'license_key'  => $license_key,
                'domain'       => $domain,
                'instance_id'  => $instance_id,
            ]),
        ]);

        if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
            $body = json_decode(wp_remote_retrieve_body($response), true);
            if (!empty($body['ok'])) {
                set_transient($this->option_key . '_verified', true, DAY_IN_SECONDS);
                return true;
            }
        }

        return false;
    }

    public function deactivate() {
        $license_key = get_option($this->option_key . '_key');
        $instance_id = $this->get_instance_id();
        $domain = parse_url(home_url(), PHP_URL_HOST);

        if ($license_key) {
            wp_remote_post($this->api_url . '/licenses/deactivate', [
                'timeout' => 10,
                'headers' => ['Content-Type' => 'application/json'],
                'body'    => json_encode([
                    'product_code' => $this->product_code,
                    'license_key'  => $license_key,
                    'domain'       => $domain,
                    'instance_id'  => $instance_id,
                ]),
            ]);
        }

        delete_option($this->option_key . '_key');
        delete_option($this->option_key . '_token');
        delete_transient($this->option_key . '_verified');
        return true;
    }

    protected function get_instance_id() {
        $id = get_option($this->option_key . '_instance_id');
        if (!$id) {
            $id = wp_generate_uuid4();
            update_option($this->option_key . '_instance_id', $id);
        }
        return $id;
    }
}