import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { clearToken, createInstance, getInstanceStats, getMe, getToken, instanceAction, listInstances, listTemplates, login, register, deleteInstance } from "./api";
import TemplateManager from "./TemplateManager.jsx";
import "./styles.css";

function Auth({ onLogin }) {
  const [mode, setMode] = useState("login"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [message, setMessage] = useState("");
  async function submit(event) { event.preventDefault(); try { if (mode === "register") { await register(email, password); setMessage("Регистрация завершена. Теперь войдите."); setMode("login"); return; } await login(email, password); onLogin(await getMe()); } catch (error) { setMessage(error.message); } }
  return <main className="page"><section className="card auth-card"><h1>Proxmox VPS Panel</h1><h2>{mode === "login" ? "Вход" : "Регистрация"}</h2><form onSubmit={submit}><label>Email<input type="email" value={email} onChange={(e) => setEmail(e.target.value)} required /></label><label>Пароль<input type="password" minLength={12} value={password} onChange={(e) => setPassword(e.target.value)} required /></label><button type="submit">{mode === "login" ? "Войти" : "Зарегистрироваться"}</button></form><button className="link" type="button" onClick={() => setMode(mode === "login" ? "register" : "login")}>{mode === "login" ? "Создать аккаунт" : "У меня уже есть аккаунт"}</button>{message && <p className="message">{message}</p>}</section></main>;
}

function formatBytes(value) { if (!value) return "0 B"; const units = ["B", "KB", "MB", "GB", "TB"]; let number = value; let index = 0; while (number >= 1024 && index < units.length - 1) { number /= 1024; index += 1; } return `${number.toFixed(index > 1 ? 1 : 0)} ${units[index]}`; }

function InstanceCard({ item, locked, onChanged }) {
  const [stats, setStats] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState("");
  async function loadStats() { try { setStats(await getInstanceStats(item.id)); } catch (e) { setError(e.message); } }
  useEffect(() => { loadStats(); const timer = setInterval(loadStats, 10000); return () => clearInterval(timer); }, [item.id]);
  async function action(name) { setBusy(true); setError(""); try { await instanceAction(item.id, name); await onChanged(); await loadStats(); } catch (e) { setError(e.message); } finally { setBusy(false); } }
  async function remove() { if (!window.confirm(`Удалить ${item.name} (VMID ${item.vmid})?`)) return; setBusy(true); try { await deleteInstance(item.id); await onChanged(); } catch (e) { setError(e.message); setBusy(false); } }
  return <div className="item instance-item"><strong>{item.name}</strong><span>{item.guest_type.toUpperCase()} · VMID {item.vmid}</span><span className="muted">{stats?.status || item.status} · {item.node}</span>{stats && <div className="stats"><span>CPU: {stats.cpu_pct}%</span><span>RAM: {formatBytes(stats.memory_used_bytes)} / {formatBytes(stats.memory_total_bytes)}</span></div>}<div className="actions">{stats?.status !== "running" && <button disabled={busy || locked} onClick={() => action("start")}>Старт</button>}{stats?.status === "running" && <button disabled={busy || locked} onClick={() => action("shutdown")}>Стоп</button>}<button disabled={busy || locked} onClick={() => action("reboot")}>Перезапуск</button><button className="danger" disabled={busy || locked} onClick={remove}>{busy ? "Выполняется..." : "Удалить"}</button></div>{error && <span className="error-text">{error}</span>}</div>;
}

function App() {
  const [user, setUser] = useState(null); const [message, setMessage] = useState(""); const [templates, setTemplates] = useState([]); const [instances, setInstances] = useState([]); const [creating, setCreating] = useState(false); const [form, setForm] = useState({ name: "", template_id: "", username: "paneluser", password: "" });
  async function refresh() { const [ts, ins] = await Promise.all([listTemplates(), listInstances()]); setTemplates(ts); setInstances(ins); setForm((current) => ({ ...current, template_id: current.template_id || String(ts[0]?.id || "") })); }
  useEffect(() => { if (!getToken()) return; Promise.all([getMe(), refresh()]).then(([current]) => setUser(current)).catch(() => clearToken()); }, []);
  async function submit(event) { event.preventDefault(); if (creating) return; setCreating(true); setMessage("Создание VPS выполняется. Повторные клики заблокированы..."); try { const result = await createInstance({ ...form, template_id: Number(form.template_id) }); setMessage(`Создан VMID ${result.vmid}. Пароль: ${result.initial_password}`); setForm((v) => ({ ...v, name: "", password: "" })); await refresh(); } catch (e) { setMessage(e.message); } finally { setCreating(false); } }
  if (!user) return <Auth onLogin={setUser} />;
  return <main className="page dashboard"><header className="topbar"><div><strong>Proxmox VPS Panel</strong><span className="muted">{user.email} · {user.role}</span></div><button onClick={() => { clearToken(); setUser(null); }}>Выйти</button></header>{message && <p className="message">{message}</p>}<section className="card"><h2>Мои инстансы</h2>{instances.length ? <div className="items">{instances.map((item) => <InstanceCard key={item.id} item={item} locked={creating} onChanged={refresh} />)}</div> : <p className="muted">Инстансов пока нет.</p>}</section><section className="card"><h2>Создать VPS</h2><form onSubmit={submit}><label>Имя VPS<input pattern="[a-zA-Z0-9][a-zA-Z0-9-]*" value={form.name} onChange={(e) => setForm({ ...form, name: e.target.value })} required /></label><label>Шаблон<select value={form.template_id} onChange={(e) => setForm({ ...form, template_id: e.target.value })} required><option value="">Выберите шаблон</option>{templates.map((item) => <option key={item.id} value={item.id}>{item.name} ({item.guest_type.toUpperCase()})</option>)}</select></label><div className="form-row"><label>Пользователь<input value={form.username} onChange={(e) => setForm({ ...form, username: e.target.value })} required /></label><label>Пароль<input type="password" minLength={12} value={form.password} onChange={(e) => setForm({ ...form, password: e.target.value })} required /></label></div><button type="submit" disabled={!templates.length || creating}>{creating ? "Создание VPS..." : "Создать VPS"}</button></form></section>{user.role === "admin" && <TemplateManager />}</main>;
}

createRoot(document.getElementById("root")).render(<React.StrictMode><App /></React.StrictMode>);
