<script>
  import { onMount } from "svelte";
  import { get } from "../lib/api.js";

  let isTailscale = location.hostname.startsWith("100.");
  let isLocal = location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname.startsWith("192.168.");
  let canAccess = isTailscale || isLocal;
  let token = $state("");

  onMount(async () => {
    if (canAccess) {
      const res = await get("/system/openclaw-token");
      token = res?.token || "";
    }
  });

  function clawUrl() {
    const base = `${location.protocol}//${location.hostname}:3100`;
    return token ? `${base}/#token=${token}` : base;
  }
</script>

<div class="space-y-4">
  <div>
    <h1 class="text-2xl font-bold text-surface-900 tracking-tight">OpenClaw</h1>
    <p class="text-sm text-surface-400 mt-1">AI Assistant</p>
  </div>

  {#if canAccess}
    <div class="flex flex-col items-center justify-center p-12 text-center bg-surface-50 dark:bg-surface-800 rounded-xl border border-surface-200 dark:border-surface-700 h-[calc(100vh-10rem)]">
      <div class="h-16 w-16 bg-primary-100 dark:bg-primary-900/30 text-primary-600 dark:text-primary-400 rounded-2xl flex items-center justify-center mb-6">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
        </svg>
      </div>
      <h2 class="text-xl font-semibold text-surface-900 dark:text-white mb-2">OpenClaw Ready</h2>
      <p class="text-surface-600 dark:text-surface-400 max-w-md mb-8">
        OpenClaw's security settings prevent it from being embedded inside the dashboard. Click below to launch the assistant securely in a new tab.
      </p>
      <a
        href={clawUrl()}
        target="_blank"
        rel="noopener noreferrer"
        class="inline-flex items-center justify-center px-6 py-3 border border-transparent text-base font-medium rounded-lg text-white bg-primary-600 hover:bg-primary-700 shadow-sm transition-colors"
      >
        Launch OpenClaw
        <svg xmlns="http://www.w3.org/2000/svg" class="ml-2 -mr-1 h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
        </svg>
      </a>
    </div>
  {:else}
    <div class="flex flex-col items-center justify-center p-12 text-center bg-surface-50 dark:bg-surface-800 rounded-xl border border-surface-200 dark:border-surface-700 h-[calc(100vh-10rem)]">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-16 w-16 text-surface-400 mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
      </svg>
      <h2 class="text-xl font-semibold text-surface-900 dark:text-white mb-2">Tailscale Network Required</h2>
      <p class="text-surface-600 dark:text-surface-400 max-w-md">
        For security reasons, OpenClaw is only accessible when connected directly to the NAS via the internal Tailscale network.
      </p>
      <p class="mt-4 text-sm text-surface-500">
        Connect to Tailscale and visit <a href="http://100.78.131.124:4000" class="text-primary-500 hover:underline font-medium">http://100.78.131.124:4000</a> to use the AI Assistant.
      </p>
    </div>
  {/if}
</div>
