import { Cpu, RefreshCw, Server, SquareTerminal } from "lucide-solid";
import { createMemo, For, Show } from "solid-js";
import { EmptyState } from "../../components/feedback/empty-state";
import { Page } from "../../components/layout/page";
import { PageHeading } from "../../components/layout/page-heading";
import { StatusBadge } from "../../components/services/status-badge";
import { Button, IconButton } from "../../components/ui/button";
import { Card } from "../../components/ui/card";
import { serviceKey } from "../../lib/model";
import { servicesFrom } from "../../lib/view-state";
import type { PageProps } from "../types";

export default function InstancesPage(props: PageProps) {
  const services = createMemo(() => servicesFrom(props.state()));
  const running = createMemo(
    () => services().filter((service) => service.status === "running").length,
  );

  return (
    <Page>
      <PageHeading
        eyebrow="ACTORS"
        title="运行实例"
        description={`${running()} 个 Actor 保持独立 Store 与串行 mailbox。`}
        actions={
          <IconButton
            aria-label="刷新"
            title="刷新"
            onClick={() => props.command({ type: "refresh" })}
          >
            <RefreshCw size={16} />
          </IconButton>
        }
      />
      <Show
        when={services().length > 0}
        fallback={
          <Card>
            <EmptyState icon={Server} title="暂无实例" detail="先注册一个组件包。" />
          </Card>
        }
      >
        <section class="grid gap-3 md:grid-cols-2 xl:grid-cols-3">
          <For each={services()}>
            {(service) => (
              <Card as="article" padding="medium">
                <div class="flex items-start justify-between gap-3">
                  <div class="flex min-w-0 items-center gap-3">
                    <span class="flex size-10 shrink-0 items-center justify-center rounded-md bg-cyan-soft text-cyan-strong">
                      <Cpu size={18} />
                    </span>
                    <div class="min-w-0">
                      <strong class="block truncate">{service.id}</strong>
                      <span class="mt-1 block truncate font-mono text-xs text-muted">
                        {service.revision}
                      </span>
                    </div>
                  </div>
                  <StatusBadge status={service.status} />
                </div>
                <dl class="mt-5 grid grid-cols-2 gap-x-4 gap-y-3 border-t border-line pt-4 text-xs">
                  <div>
                    <dt class="text-muted">内存上限</dt>
                    <dd class="mt-1 font-semibold">{service.memoryMb} MB</dd>
                  </div>
                  <div>
                    <dt class="text-muted">Mailbox</dt>
                    <dd class="mt-1 font-semibold">{service.mailbox}</dd>
                  </div>
                  <div>
                    <dt class="text-muted">Deadline</dt>
                    <dd class="mt-1 font-semibold">{service.deadlineMs} ms</dd>
                  </div>
                  <div>
                    <dt class="text-muted">最近延迟</dt>
                    <dd class="mt-1 font-semibold">
                      {service.latencyMs === null ? "—" : `${service.latencyMs} ms`}
                    </dd>
                  </div>
                </dl>
                <div class="mt-5 flex justify-end gap-2">
                  <Show when={service.status === "running"}>
                    <Button
                      size="small"
                      onClick={() =>
                        props.command({
                          type: "open-invoke",
                          serviceKey: serviceKey(service),
                        })
                      }
                    >
                      <SquareTerminal size={15} />
                      调用
                    </Button>
                  </Show>
                  <Button
                    size="small"
                    disabled={props.state()?.serviceAction !== null}
                    onClick={() =>
                      props.command({
                        type: "service-action",
                        serviceKey: serviceKey(service),
                        action: service.status === "running" ? "stop" : "start",
                      })
                    }
                  >
                    {service.status === "running" ? "停止" : "启动"}
                  </Button>
                </div>
              </Card>
            )}
          </For>
        </section>
      </Show>
    </Page>
  );
}
