"use client";

import { useState } from "react";
import { c2api } from "@/lib/c2api";
import type { NodeRecord, SystemRecord } from "@/lib/types";

interface Props {
  node: NodeRecord;
  systems: SystemRecord[];
  onClose: () => void;
}

export function NodeConfigModal({ node, systems, onClose }: Props) {
  const [systemId, setSystemId] = useState("");
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault();
    if (!systemId) return;
    setSaving(true);
    setError(null);
    try {
      await c2api.assignSystem(node.node_id, systemId);
      onClose();
    } catch (err) {
      setError(err instanceof Error ? err.message : "Failed to assign system.");
    } finally {
      setSaving(false);
    }
  }

  return (
    <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50">
      <div className="bg-gray-900 border border-gray-700 rounded-xl p-6 w-full max-w-md font-mono">
        <h2 className="text-white font-semibold mb-1">Configure Node</h2>
        <p className="text-gray-400 text-sm mb-5">
          <span className="text-indigo-400">{node.node_id}</span> connected for the first time.
          Assign it a radio system to begin monitoring.
        </p>

        <form onSubmit={handleSubmit} className="space-y-4">
          <div>
            <label className="block text-xs text-gray-400 mb-1">Radio System</label>
            <select
              value={systemId}
              onChange={(e) => setSystemId(e.target.value)}
              className="w-full bg-gray-800 border border-gray-700 rounded-lg px-3 py-2 text-white text-sm focus:outline-none focus:border-indigo-500"
              required
            >
              <option value="">Select a system…</option>
              {systems.map((s) => (
                <option key={s.system_id} value={s.system_id}>
                  {s.name} ({s.type})
                </option>
              ))}
            </select>
          </div>

          {error && <p className="text-red-400 text-xs">{error}</p>}

          <div className="flex gap-3 pt-1">
            <button
              type="submit"
              disabled={saving || !systemId}
              className="flex-1 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 text-white rounded-lg py-2 text-sm font-semibold transition-colors"
            >
              {saving ? "Saving…" : "Assign & Configure"}
            </button>
            <button
              type="button"
              onClick={onClose}
              className="flex-1 bg-gray-800 hover:bg-gray-700 text-gray-300 rounded-lg py-2 text-sm transition-colors"
            >
              Cancel
            </button>
          </div>
        </form>
      </div>
    </div>
  );
}
