/**
 * Tauri-backed device gateway (ticket #77, lot F2).
 *
 * The desktop app hosts the embedded server, so these commands reach the same
 * device use cases through the composition root — **never** over HTTP to its own
 * server (carnet #77, cadrage Architecture).
 *
 * Command names and envelopes are those B3 landed (`commands.rs`).
 */

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

import type { PairedDevice, PairingCode } from "@/domain";
import type { DeviceGateway } from "@/ports";

/** Backend `DeviceListDto` — the list is wrapped, not returned bare. */
interface DeviceListDto {
  devices: PairedDevice[];
}

export class TauriDeviceGateway implements DeviceGateway {
  async listDevices(): Promise<PairedDevice[]> {
    const dto = await invoke<DeviceListDto>("list_devices");
    return dto.devices;
  }

  createPairingCode(): Promise<PairingCode> {
    return invoke<PairingCode>("create_pairing_code");
  }

  renameDevice(deviceId: string, name: string): Promise<void> {
    return invoke<void>("rename_device", { request: { deviceId, name } });
  }

  revokeDevice(deviceId: string): Promise<void> {
    return invoke<void>("revoke_device", { request: { deviceId } });
  }

  revokeAllDevices(): Promise<void> {
    return invoke<void>("revoke_all_devices");
  }
}
