import { Database, Globe2, Save, ShieldCheck } from "lucide-solid";
import { createEffect, createSignal } from "solid-js";
import { displayApiBase } from "../../lib/api";
import { Page } from "../../components/layout/page";
import { PageHeading } from "../../components/layout/page-heading";
import { Button } from "../../components/ui/button";
import { Card, CardHeader } from "../../components/ui/card";
import { Field, FieldLabel } from "../../components/ui/field";
import { Input } from "../../components/ui/input";
import type { PageProps } from "../types";

export default function SettingsPage(props: PageProps) {
  const [endpoint, setEndpoint] = createSignal("");
  let initialized = false;
  createEffect(() => {
    const state = props.state();
    if (state && !initialized) {
      setEndpoint(state.apiBase);
      initialized = true;
    }
  });

  return (
    <Page>
      <PageHeading
        eyebrow="CONFIGURATION"
        title="运行设置"
        description="配置管理 API 连接；运行边界由后端策略统一控制。"
      />
      <div class="grid gap-5 xl:grid-cols-[minmax(0,1.4fr)_minmax(280px,0.7fr)]">
        <Card as="section">
          <CardHeader>
            <div>
              <h2>管理 API</h2>
              <p>留空表示使用当前页面同源地址</p>
            </div>
            <Globe2 size={18} class="text-cyan-strong" />
          </CardHeader>
          <form
            class="p-5"
            onSubmit={(event) => {
              event.preventDefault();
              props.command({ type: "save-api-base", value: endpoint() });
            }}
          >
            <Field>
              <FieldLabel>Endpoint</FieldLabel>
              <Input
                type="url"
                value={endpoint()}
                placeholder={window.location.origin}
                onInput={(event) => setEndpoint(event.currentTarget.value)}
              />
            </Field>
            <p class="mt-2 text-xs text-muted">
              当前解析地址：{displayApiBase(props.state()?.apiBase ?? "")}
            </p>
            <div class="mt-5 flex justify-end">
              <Button variant="primary" type="submit">
                <Save size={15} />
                保存连接
              </Button>
            </div>
          </form>
        </Card>
        <aside class="space-y-3">
          <Info
            icon={ShieldCheck}
            title="Sandbox"
            detail="网络与 Host 能力默认拒绝，按 WIT import 精确链接。"
          />
          <Info icon={Database} title="持久化" detail="控制状态和服务 KV 存储于本地 libSQL。" />
        </aside>
      </div>
    </Page>
  );
}

function Info(props: { icon: typeof ShieldCheck; title: string; detail: string }) {
  const Icon = props.icon;
  return (
    <Card as="article" padding="small" class="flex gap-3">
      <span class="flex size-9 shrink-0 items-center justify-center rounded-md bg-brand-soft text-brand">
        <Icon size={16} />
      </span>
      <div>
        <strong class="text-sm">{props.title}</strong>
        <p class="mt-1 text-xs leading-5 text-muted">{props.detail}</p>
      </div>
    </Card>
  );
}
