import { invoke } from "@tauri-apps/api/core";

export type Edition = "free" | "base" | "premium";

export interface LicenseInfo {
  edition: Edition;
  email: string;
  features: string[];
  machine_limit: number;
  issued_at: number;
  expires_at: number;
}

export async function validateLicenseKey(key: string): Promise<LicenseInfo> {
  return invoke<LicenseInfo>("validate_license_key", { key });
}

export async function storeLicense(key: string): Promise<LicenseInfo> {
  return invoke<LicenseInfo>("store_license", { key });
}

export async function readLicense(): Promise<LicenseInfo | null> {
  return invoke<LicenseInfo | null>("read_license");
}

export async function getEdition(): Promise<Edition> {
  return invoke<Edition>("get_edition");
}

export async function getMachineId(): Promise<string> {
  return invoke<string>("get_machine_id");
}

export async function checkEntitlement(feature: string): Promise<boolean> {
  return invoke<boolean>("check_entitlement", { feature });
}

export interface MachineInfo {
  machine_id: string;
  machine_name: string | null;
  activated_at: string;
  last_seen_at: string;
}

export interface ActivationStatus {
  is_activated: boolean;
  machine_id: string;
}

export async function activateMachine(): Promise<void> {
  return invoke<void>("activate_machine");
}

export async function deactivateMachine(machineId: string): Promise<void> {
  return invoke<void>("deactivate_machine", { machineId });
}

export async function listActivatedMachines(): Promise<MachineInfo[]> {
  return invoke<MachineInfo[]>("list_activated_machines");
}

export async function getActivationStatus(): Promise<ActivationStatus> {
  return invoke<ActivationStatus>("get_activation_status");
}
