import { useMemo, useState } from 'react';

import {
  CheckCircle2,
  KeyRound,
  Link2,
  MonitorCog,
  Save,
  Server,
  ShieldCheck,
} from 'lucide-react';

import { Badge } from '@/components/ui/Badge';
import { Button } from '@/components/ui/Button';
import { Card } from '@/components/ui/Card';

import {
  getSettings,
  saveSettings,
} from '@/services/apiClient';

const DEFAULT_OVERLAY_ROUTE = '198.19.0.0/16';
const DEFAULT_ROUTER_IP = '198.51.100.1';
const DEFAULT_CONTROLLER_IP = '198.51.100.10';
const DEFAULT_PLC_IP = '198.51.100.50';
const DEFAULT_FIRMWARE =
  'openwrt-23.05.5-zbt-we826-16m-litoral-golden-sysupgrade.bin';

export function BackendSettings() {
  const [settings, setSettings] =
    useState(getSettings());

  const [saved, setSaved] = useState(false);

  const backendHost = useMemo(() => {
    try {
      return new URL(settings.backendUrl).host;
    } catch {
      return 'URL backend inválido';
    }
  }, [settings.backendUrl]);

  function handleSave() {
    saveSettings(settings);
    setSaved(true);

    window.setTimeout(() => {
      setSaved(false);
    }, 2500);
  }

  return (
    <div className="flex h-full flex-col overflow-hidden">
      <div className="mb-6 flex items-start justify-between">
        <div>
          <h1 className="text-3xl font-bold tracking-tight text-white">
            Posto de Trabalho
          </h1>

          <p className="mt-1 text-slate-400">
            Configuração local da consola técnica
            para provisionamento de routers.
          </p>
        </div>

        <Badge tone={saved ? 'green' : 'blue'}>
          {saved ? 'Guardado' : 'Configuração Local'}
        </Badge>
      </div>

      <div className="grid min-h-0 flex-1 grid-cols-12 gap-5 overflow-hidden">
        <Card className="col-span-7 flex flex-col">
          <div className="mb-6 flex items-center gap-4">
            <div className="rounded-2xl bg-blue-500/10 p-3 text-blue-300">
              <Server size={24} />
            </div>

            <div>
              <h2 className="text-xl font-semibold text-white">
                Ligação ao Backend
              </h2>

              <p className="text-sm text-slate-400">
                Endpoint API usado para atribuição
                de IP VPN e registo de peers.
              </p>
            </div>
          </div>

          <div className="space-y-5">
            <div>
              <label className="mb-2 block text-xs font-semibold uppercase tracking-wide text-slate-500">
                URL do Backend
              </label>

              <div className="relative">
                <Link2
                  size={16}
                  className="absolute left-4 top-1/2 -translate-y-1/2 text-blue-300/70"
                />

                <input
                  value={settings.backendUrl}
                  onChange={(event) =>
                    setSettings({
                      ...settings,
                      backendUrl: event.target.value,
                    })
                  }
                  className="w-full rounded-2xl border border-white/10 bg-slate-950/80 py-4 pl-11 pr-4 font-mono text-sm text-white outline-none transition placeholder:text-slate-600 focus:border-blue-500/50 focus:bg-slate-950"
                  placeholder="http://localhost:8080"
                />
              </div>
            </div>

            <div>
              <label className="mb-2 block text-xs font-semibold uppercase tracking-wide text-slate-500">
                Chave API
              </label>

              <div className="relative">
                <KeyRound
                  size={16}
                  className="absolute left-4 top-1/2 -translate-y-1/2 text-blue-300/70"
                />

                <input
                  value={settings.apiKey}
                  onChange={(event) =>
                    setSettings({
                      ...settings,
                      apiKey: event.target.value,
                    })
                  }
                  className="w-full rounded-2xl border border-white/10 bg-slate-950/80 py-4 pl-11 pr-4 font-mono text-sm text-white outline-none transition placeholder:text-slate-600 focus:border-blue-500/50 focus:bg-slate-950"
                  placeholder="dev-api-key"
                />
              </div>
            </div>
          </div>

          <div className="mt-6 rounded-2xl border border-white/10 bg-white/[0.025] p-5">
            <div className="flex items-start justify-between gap-4">
              <div>
                <p className="text-sm font-semibold text-white">
                  Alvo Ligado
                </p>

                <p className="mt-1 font-mono text-sm text-slate-400">
                  {backendHost}
                </p>
              </div>

              <Badge tone="green">
                Pronto
              </Badge>
            </div>
          </div>

          <div className="mt-auto flex justify-end pt-6">
            <button
              type="button"
              onClick={handleSave}
              className="group inline-flex items-center gap-2 rounded-2xl border border-white/10 bg-white/[0.02] px-5 py-3 text-sm font-semibold text-white transition-all duration-200 hover:-translate-y-[1px] hover:border-blue-500/30 hover:bg-blue-500/10 active:translate-y-0"
            >
              <Save
                size={16}
                className="transition-transform duration-200 group-hover:rotate-6"
              />

              <span className="transition-colors group-hover:text-blue-200">
                Guardar Configuração
              </span>
            </button>
          </div>
        </Card>

        <div className="col-span-5 flex min-h-0 flex-col gap-5">
          <Card>
            <div className="mb-5 flex items-center gap-4">
              <div className="rounded-2xl bg-purple-500/10 p-3 text-purple-300">
                <MonitorCog size={24} />
              </div>

              <div>
                <h2 className="text-xl font-semibold text-white">
                  Base de Provisionamento
                </h2>

                <p className="text-sm text-slate-400">
                  Valores de produção apenas leitura.
                </p>
              </div>
            </div>

            <div className="grid gap-3">
              <InfoRow
                label="Rota Overlay"
                value={DEFAULT_OVERLAY_ROUTE}
              />

              <InfoRow
                label="IP LAN Router"
                value={DEFAULT_ROUTER_IP}
              />

              <InfoRow
                label="IP Controlador"
                value={DEFAULT_CONTROLLER_IP}
              />

              <InfoRow
                label="IP PLC"
                value={DEFAULT_PLC_IP}
              />

              <InfoRow
                label="Firmware Alvo"
                value={DEFAULT_FIRMWARE}
              />
            </div>
          </Card>

          <Card className="flex-1">
            <div className="flex items-start gap-4">
              <div className="rounded-2xl bg-green-500/10 p-3 text-green-300">
                <ShieldCheck size={24} />
              </div>

              <div>
                <h2 className="text-xl font-semibold text-white">
                  Perfil de Produção
                </h2>

                <p className="mt-3 text-sm leading-6 text-slate-400">
                  Base OpenWrt 23.05,
                  alvo ZBT-WE826 16M,
                  firewall fw4/nftables,
                  LuCI sobre WireGuard, topologia
                  LAN estável e registo automático
                  de peers VPS.
                </p>

                <div className="mt-5 flex flex-wrap gap-2">
                  <Badge tone="blue">
                    OpenWrt 23.05
                  </Badge>

                  <Badge tone="purple">
                    WireGuard
                  </Badge>

                  <Badge tone="green">
                    fw4/nftables
                  </Badge>
                </div>
              </div>
            </div>
          </Card>

          {saved && (
            <div className="rounded-2xl border border-green-500/20 bg-green-500/10 p-4 text-sm text-green-300">
              <div className="flex items-center gap-2">
                <CheckCircle2 size={16} />
                Configuração do posto de trabalho guardada.
              </div>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}

function InfoRow({
  label,
  value,
}: {
  label: string;
  value: string;
}) {
  return (
    <div className="rounded-2xl border border-white/10 bg-slate-950/70 p-4">
      <p className="text-xs font-semibold uppercase tracking-wide text-slate-500">
        {label}
      </p>

      <p className="mt-2 break-all font-mono text-sm text-slate-100">
        {value}
      </p>
    </div>
  );
}