import { useState } from "react";
import { useTranslation } from "react-i18next";
import {
  Activity,
  HardDrive,
  LayoutDashboard,
  Plus,
  Server,
  Trash2,
  Zap,
} from "lucide-react";

import { Button } from "@/components/button";
import { Input } from "@/components/input";
import { SectionCard, SettingRow, FakeSwitch } from "@/components/section-card";
import type { HostEditorForm } from "./HostEditorData";

type SetHostField = <K extends keyof HostEditorForm>(
  key: K,
  value: HostEditorForm[K],
) => void;

export function HostStatsTab({
  form,
  setField,
  snippets,
}: {
  form: HostEditorForm;
  setField: SetHostField;
  snippets: { id: number; name: string }[];
}) {
  const { t } = useTranslation();
  const [newMount, setNewMount] = useState("");
  const [newMonitoredPath, setNewMonitoredPath] = useState("");
  const [newMonitoredLabel, setNewMonitoredLabel] = useState("");
  const excludedMounts = form.statsConfig.excludedMounts ?? [];
  const monitoredMounts = form.statsConfig.monitoredMounts ?? [];

  const addExcludedMount = () => {
    const value = newMount.trim();
    if (!value || excludedMounts.includes(value)) {
      setNewMount("");
      return;
    }
    setField("statsConfig", {
      ...form.statsConfig,
      excludedMounts: [...excludedMounts, value],
    });
    setNewMount("");
  };

  const addMonitoredMount = () => {
    const path = newMonitoredPath.trim();
    if (!path || monitoredMounts.some((entry) => entry.path === path)) return;
    setField("statsConfig", {
      ...form.statsConfig,
      monitoredMounts: [
        ...monitoredMounts,
        { path, label: newMonitoredLabel.trim() || undefined },
      ],
    });
    setNewMonitoredPath("");
    setNewMonitoredLabel("");
  };

  return (
    <>
      <SectionCard
        title={t("hosts.statusChecksLabel")}
        icon={<Activity className="size-3.5" />}
      >
        <div className="flex flex-col gap-0 py-1">
          <SettingRow
            label={t("hosts.enableStatusChecks")}
            description={t("hosts.enableStatusChecksDesc")}
          >
            <FakeSwitch
              checked={form.statsConfig.statusCheckEnabled}
              onChange={(v) =>
                setField("statsConfig", {
                  ...form.statsConfig,
                  statusCheckEnabled: v,
                })
              }
            />
          </SettingRow>
          {form.statsConfig.statusCheckEnabled && (
            <SettingRow
              label={t("hosts.useGlobalInterval")}
              description={t("hosts.useGlobalIntervalDesc")}
            >
              <FakeSwitch
                checked={form.statsConfig.useGlobalStatusInterval}
                onChange={(v) =>
                  setField("statsConfig", {
                    ...form.statsConfig,
                    useGlobalStatusInterval: v,
                  })
                }
              />
            </SettingRow>
          )}
          {form.statsConfig.statusCheckEnabled &&
            !form.statsConfig.useGlobalStatusInterval && (
              <SettingRow
                label={t("hosts.checkIntervalS")}
                description={t("hosts.checkIntervalDesc")}
              >
                <Input
                  type="number"
                  value={form.statsConfig.statusCheckInterval}
                  onChange={(e) =>
                    setField("statsConfig", {
                      ...form.statsConfig,
                      statusCheckInterval: Number(e.target.value),
                    })
                  }
                  className="w-20 h-7 text-xs text-right [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
                />
              </SettingRow>
            )}
        </div>
      </SectionCard>
      <SectionCard
        title={t("hosts.metricsCollectionLabel")}
        icon={<Server className="size-3.5" />}
      >
        <div className="flex flex-col gap-0 py-1">
          <SettingRow
            label={t("hosts.enableMetricsLabel")}
            description={t("hosts.enableMetricsDesc")}
          >
            <FakeSwitch
              checked={form.statsConfig.metricsEnabled}
              onChange={(v) =>
                setField("statsConfig", {
                  ...form.statsConfig,
                  metricsEnabled: v,
                })
              }
            />
          </SettingRow>
          {form.statsConfig.metricsEnabled && (
            <SettingRow
              label={t("hosts.useGlobalMetrics")}
              description={t("hosts.useGlobalMetricsDesc")}
            >
              <FakeSwitch
                checked={form.statsConfig.useGlobalMetricsInterval}
                onChange={(v) =>
                  setField("statsConfig", {
                    ...form.statsConfig,
                    useGlobalMetricsInterval: v,
                  })
                }
              />
            </SettingRow>
          )}
          {form.statsConfig.metricsEnabled &&
            !form.statsConfig.useGlobalMetricsInterval && (
              <SettingRow
                label={t("hosts.metricsIntervalS")}
                description={t("hosts.metricsIntervalDesc2")}
              >
                <Input
                  type="number"
                  value={form.statsConfig.metricsInterval}
                  onChange={(e) =>
                    setField("statsConfig", {
                      ...form.statsConfig,
                      metricsInterval: Number(e.target.value),
                    })
                  }
                  className="w-20 h-7 text-xs text-right [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
                />
              </SettingRow>
            )}
        </div>
      </SectionCard>
      <SectionCard
        title={t("hosts.monitoredMountsLabel")}
        icon={<HardDrive className="size-3.5" />}
      >
        <div className="flex flex-col gap-3 py-3">
          <p className="text-xs text-muted-foreground">
            {t("hosts.monitoredMountsDesc")}
          </p>
          <div className="grid grid-cols-[1fr_0.7fr_auto] gap-2">
            <Input
              className="h-7 text-xs"
              placeholder={t("hosts.monitoredMountPathPlaceholder")}
              value={newMonitoredPath}
              onChange={(e) => setNewMonitoredPath(e.target.value)}
              onKeyDown={(e) => {
                if (e.key === "Enter") {
                  e.preventDefault();
                  addMonitoredMount();
                }
              }}
            />
            <Input
              className="h-7 text-xs"
              placeholder={t("hosts.monitoredMountLabelPlaceholder")}
              value={newMonitoredLabel}
              onChange={(e) => setNewMonitoredLabel(e.target.value)}
              onKeyDown={(e) => {
                if (e.key === "Enter") {
                  e.preventDefault();
                  addMonitoredMount();
                }
              }}
            />
            <Button
              variant="outline"
              size="sm"
              className="h-7 px-2 text-[10px]"
              onClick={addMonitoredMount}
            >
              <Plus className="mr-1 size-3" /> {t("hosts.addMonitoredMount")}
            </Button>
          </div>
          {monitoredMounts.map((entry) => (
            <div
              key={entry.path}
              className="flex items-center gap-2 border border-border bg-muted/20 p-2 group"
            >
              <span className="min-w-0 flex-1 truncate font-mono text-xs">
                {entry.path}
              </span>
              {entry.label && (
                <span className="truncate text-xs text-muted-foreground">
                  {entry.label}
                </span>
              )}
              <button
                className="text-muted-foreground opacity-0 transition-opacity hover:text-destructive group-hover:opacity-100"
                onClick={() =>
                  setField("statsConfig", {
                    ...form.statsConfig,
                    monitoredMounts: monitoredMounts.filter(
                      (mount) => mount.path !== entry.path,
                    ),
                  })
                }
              >
                <Trash2 className="size-3.5" />
              </button>
            </div>
          ))}
        </div>
      </SectionCard>
      <SectionCard
        title={t("hosts.excludedMountsLabel")}
        icon={<HardDrive className="size-3.5" />}
      >
        <div className="flex flex-col gap-3 py-3">
          <p className="text-xs text-muted-foreground">
            {t("hosts.excludedMountsDesc")}
          </p>
          <div className="flex items-center gap-2">
            <Input
              className="h-7 text-xs flex-1"
              placeholder={t("hosts.excludedMountsPlaceholder")}
              value={newMount}
              onChange={(e) => setNewMount(e.target.value)}
              onKeyDown={(e) => {
                if (e.key === "Enter") {
                  e.preventDefault();
                  addExcludedMount();
                }
              }}
            />
            <Button
              variant="outline"
              size="sm"
              className="h-7 text-[10px] px-2 border-accent-brand/40 text-accent-brand"
              onClick={addExcludedMount}
            >
              <Plus className="size-3 mr-1" /> {t("hosts.addExcludedMount")}
            </Button>
          </div>
          {excludedMounts.length === 0 && (
            <div className="flex flex-col items-center justify-center py-4 text-muted-foreground/40 gap-1.5">
              <HardDrive className="size-6" />
              <span className="text-xs">{t("hosts.excludedMountsEmpty")}</span>
            </div>
          )}
          {excludedMounts.map((mount, i) => (
            <div
              key={`${mount}-${i}`}
              className="flex items-center gap-2 p-2 bg-muted/20 border border-border group"
            >
              <span className="text-xs flex-1 font-mono truncate">{mount}</span>
              <button
                className="text-muted-foreground hover:text-destructive opacity-0 group-hover:opacity-100 transition-opacity"
                onClick={() =>
                  setField("statsConfig", {
                    ...form.statsConfig,
                    excludedMounts: excludedMounts.filter(
                      (_, idx) => idx !== i,
                    ),
                  })
                }
              >
                <Trash2 className="size-3.5" />
              </button>
            </div>
          ))}
        </div>
      </SectionCard>
      <SectionCard
        title={t("hosts.visibleWidgets")}
        icon={<LayoutDashboard className="size-3.5" />}
      >
        <div className="flex flex-col gap-2 py-3">
          <p className="text-xs text-muted-foreground">
            {t("hosts.widgetsMovedToHostMetrics")}
          </p>
        </div>
      </SectionCard>
      <SectionCard
        title={t("hosts.quickActionsLabel")}
        icon={<Zap className="size-3.5" />}
        action={
          <Button
            variant="outline"
            size="sm"
            className="h-6 text-[10px] px-2 border-accent-brand/40 text-accent-brand"
            onClick={() =>
              setField("quickActions", [
                ...form.quickActions,
                { name: "", snippetId: "" },
              ])
            }
          >
            <Plus className="size-3 mr-1" /> {t("hosts.addActionBtn")}
          </Button>
        }
      >
        <div className="flex flex-col gap-3 py-3">
          <p className="text-xs text-muted-foreground">
            {t("hosts.quickActionsToolbar")}
          </p>
          {form.quickActions.length === 0 && (
            <div className="flex flex-col items-center justify-center py-4 text-muted-foreground/40 gap-1.5">
              <Zap className="size-6" />
              <span className="text-xs">{t("hosts.noQuickActions")}</span>
            </div>
          )}
          {form.quickActions.map((a, i) => (
            <div
              key={i}
              className="flex items-center gap-2 p-2 bg-muted/20 border border-border group"
            >
              <Input
                className="h-7 text-xs flex-1"
                placeholder={t("hosts.buttonLabel")}
                value={a.name}
                onChange={(e) => {
                  const updated = [...form.quickActions];
                  updated[i] = { ...updated[i], name: e.target.value };
                  setField("quickActions", updated);
                }}
              />
              <select
                className="h-7 text-xs flex-1 border border-border bg-background px-2 outline-none focus:ring-1 focus:ring-ring"
                value={a.snippetId}
                onChange={(e) => {
                  const updated = [...form.quickActions];
                  updated[i] = {
                    ...updated[i],
                    snippetId: e.target.value,
                  };
                  setField("quickActions", updated);
                }}
              >
                <option value="">{t("hosts.selectSnippetPlaceholder")}</option>
                {snippets.map((s) => (
                  <option key={s.id} value={String(s.id)}>
                    {s.name}
                  </option>
                ))}
              </select>
              <button
                className="text-muted-foreground hover:text-destructive opacity-0 group-hover:opacity-100 transition-opacity"
                onClick={() =>
                  setField(
                    "quickActions",
                    form.quickActions.filter((_, idx) => idx !== i),
                  )
                }
              >
                <Trash2 className="size-3.5" />
              </button>
            </div>
          ))}
        </div>
      </SectionCard>
    </>
  );
}
