// NKSC1-c8c7ea42327292095dea8150adbebc85-0708f3a4174e426d0d2ad28718b26adb4894e3d58893d8e7340ae63a514fbf41

package license

import "encoding/json"

// ChatLoopLicenseRequest proves possession of an installation-specific key.
// The counter is committed with the device binding to reject captured requests.
type ChatLoopLicenseRequest struct {
	LicenseKey          string `json:"license_key"`
	MachineHash         string `json:"machine_hash"`
	LegacyMachineHash   string `json:"legacy_machine_hash,omitempty"`
	HardwareFingerprint string `json:"hardware_fingerprint"`
	DevicePublicKey     string `json:"device_public_key"`
	ProductCode         string `json:"product_code"`
	RequiredEntitlement string `json:"required_entitlement"`
	Nonce               string `json:"nonce"`
	Counter             uint64 `json:"counter"`
	IssuedAt            int64  `json:"issued_at"`
	Proof               string `json:"proof"`
}

// Keep this wire contract in sync with LMS structs/chatloop_license.go.
type ChatLoopDeviceProof struct {
	Purpose             string `json:"purpose"`
	LicenseKeyHash      string `json:"license_key_hash"`
	MachineHash         string `json:"machine_hash"`
	LegacyMachineHash   string `json:"legacy_machine_hash"`
	HardwareFingerprint string `json:"hardware_fingerprint"`
	DevicePublicKey     string `json:"device_public_key"`
	ProductCode         string `json:"product_code"`
	RequiredEntitlement string `json:"required_entitlement"`
	Nonce               string `json:"nonce"`
	Counter             uint64 `json:"counter"`
	IssuedAt            int64  `json:"issued_at"`
}

type ChatLoopLease struct {
	Version             int      `json:"version"`
	LicenseKeyHash      string   `json:"license_key_hash"`
	MachineHash         string   `json:"machine_hash"`
	HardwareFingerprint string   `json:"hardware_fingerprint"`
	DevicePublicKey     string   `json:"device_public_key"`
	RequestNonce        string   `json:"request_nonce"`
	Counter             uint64   `json:"counter"`
	IssuedAt            int64    `json:"issued_at"`
	RefreshAfter        int64    `json:"refresh_after"`
	ExpiresAt           int64    `json:"expires_at"`
	Valid               bool     `json:"valid"`
	Status              string   `json:"status"`
	ProductCode         string   `json:"product_code"`
	Entitlements        []string `json:"entitlements"`
	PackageType         string   `json:"package_type"`
	ResetRemaining      int      `json:"reset_remaining"`
	MachinesUsed        int      `json:"machines_used"`
	MachineMax          int      `json:"machine_max"`
	Message             string   `json:"message"`
}

// Payload is signed as bytes. Clients verify it before decoding any claims.
type SignedChatLoopLease struct {
	Payload   json.RawMessage `json:"payload"`
	Signature string          `json:"signature"`
	KeyID     string          `json:"key_id"`
}
