"""Safe public Quickstart.

Replace every placeholder with deployment values supplied by your
administrator before running this file. The public Web does not publish SDK
wheels, Product IDs, public keys, or License Keys.
"""
from __future__ import annotations

import getpass

from keygen_device_sdk import ClientConfig, DeviceLicenseClient, LicenseSDKError


PLACEHOLDER_VALUES = {
    "https://license.example.com",
    "YOUR-PRODUCT-UUID",
    "com.example.your-product",
    "YOUR-GATEWAY-PUBLIC-KEY",
}


def build_client() -> DeviceLicenseClient:
    config = ClientConfig(
        gateway_base_url="https://license.example.com",
        product_id="YOUR-PRODUCT-UUID",
        product_namespace="com.example.your-product",
        gateway_signing_public_key_b64="YOUR-GATEWAY-PUBLIC-KEY",
        gateway_signing_key_id="gateway-v1",
        connection_log_enabled=True,
    )
    configured_values = {
        config.gateway_base_url,
        config.product_id,
        config.product_namespace,
        config.gateway_signing_public_key_b64,
    }
    if configured_values & PLACEHOLDER_VALUES:
        raise SystemExit(
            "Replace the public placeholders with values supplied by your administrator before running this example."
        )
    return DeviceLicenseClient(config)


def main() -> int:
    client = build_client()
    try:
        key = (
            getpass.getpass("License key (first activation only): ").strip()
            if client.requires_license_key
            else None
        )
        state = client.ensure_registered(key).require_valid()
        print("License valid:", state.valid)
        print("Device ID:", state.device_id)
    except LicenseSDKError as exc:
        raise SystemExit(f"License startup failed: {exc}") from exc
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
