<template>
  <div class="flex flex-col h-full p-4 gap-3">
    <div class="flex items-center gap-2">
      <input v-model="target" type="text" placeholder="Host to trace" class="px-3 py-1.5 text-sm rounded bg-[#161b22] border border-[#30363d] text-[#e0e0e0] outline-none focus:border-[#58a6ff] flex-1" @keydown.enter="trace" />
      <button class="px-4 py-1.5 text-sm font-bold rounded bg-[#58a6ff] text-black cursor-pointer disabled:opacity-40" :disabled="running" @click="trace">Trace</button>
    </div>
    <pre class="flex-1 overflow-auto bg-[#161b22] border border-[#30363d] rounded p-3 text-xs font-mono whitespace-pre-wrap text-[#e0e0e0]">{{ output || "Enter a host and click Trace" }}</pre>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { invoke } from "@tauri-apps/api/core";

const props = defineProps<{ sessionId: string }>();
const target = ref("");
const output = ref("");
const running = ref(false);

async function trace(): Promise<void> {
  if (!target.value) return;
  running.value = true;
  output.value = `Tracing route to ${target.value}...\n`;
  try {
    output.value = await invoke<string>("tool_traceroute", { sessionId: props.sessionId, target: target.value });
  } catch (err) { output.value = String(err); }
  running.value = false;
}
</script>
