"""
Diinta License Hub — Python Client SDK
Usage:
    from diinta_license import DiintaClient
    client = DiintaClient("YOUR_PRODUCT_CODE")
    if client.verify("XXXX-XXXX-XXXX-XXXX"):
        print("License active!")
"""

import requests
import uuid
import platform
import os

class DiintaClient:
    def __init__(self, product_code: str, api_url: str = "https://diinta.so/api/v1/public"):
        self.product_code = product_code
        self.api_url = api_url.rstrip("/")
        self.machine_id = self._get_machine_id()

    def _get_machine_id(self) -> str:
        try:
            return str(uuid.getnode())
        except Exception:
            return platform.node() or "default-python-node"

    def activate(self, license_key: str, domain: str = "cli") -> dict:
        url = f"{self.api_url}/licenses/activate"
        payload = {
            "product_code": self.product_code,
            "license_key": license_key.strip(),
            "domain": domain,
            "machine_id": self.machine_id,
        }
        res = requests.post(url, json=payload, timeout=10)
        return res.json()

    def verify(self, license_key: str, domain: str = "cli") -> bool:
        url = f"{self.api_url}/licenses/verify"
        payload = {
            "product_code": self.product_code,
            "license_key": license_key.strip(),
            "domain": domain,
            "machine_id": self.machine_id,
        }
        try:
            res = requests.post(url, json=payload, timeout=8)
            data = res.json()
            return res.status_code == 200 and data.get("ok") is True
        except Exception:
            return False