'use client';
import Image from "next/image";
import { useRef, useState } from "react";


export default function Home() {
const nameInputRef = useRef<HTMLInputElement>(null);

const addPlayer = async () => {
    const playerName = nameInputRef.current?.value?.trim();
    if (!playerName) {
      alert("Please enter a player name");
      return;
    }

    try {
      const res = await fetch('http://localhost:8080/add', {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ name: playerName, group: selectedGroup }),
      });

      if (!res.ok) {
        throw new Error(`Server error: ${res.statusText}`);
      }

      if (nameInputRef.current) {
        nameInputRef.current.value = "";
      }
    } catch (error) {
      console.error("Failed to add player:", error);
    }
  };
  const [selectedGroup, setSelectedGroup] = useState<"stripes" | "solids">("stripes");

  const advanceTurn = async () => {
    await fetch('http://localhost:8080/next'); // triggers backend to emit event
  };

  const resetGame = async () => {
    await fetch('http://localhost:8080/reset'); // triggers backend to emit event
  };
  
  return (
    <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
      <main className="flex flex-col gap-[32px] row-start-2 items-center sm:items-start">
        <div className="flex gap-8 mb-4">
          <label className="flex items-center gap-2 cursor-pointer">
            <input
              type="radio"
              name="group"
              value="stripes"
              checked={selectedGroup === "stripes"}
              onChange={() => setSelectedGroup("stripes")}
              className="cursor-pointer"
            />
            Stripes
          </label>

          <label className="flex items-center gap-2 cursor-pointer">
            <input
              type="radio"
              name="group"
              value="solids"
              checked={selectedGroup === "solids"}
              onChange={() => setSelectedGroup("solids")}
              className="cursor-pointer"
            />
            Solids
          </label>
        </div>
        <div className="flex gap-4 items-center flex-col sm:flex-row">
          <input ref={nameInputRef} id="name" placeholder="Enter Name Here"></input>
          <button
            onClick={addPlayer}
            className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:w-auto">Add Player</button>
        </div>
        

        <div className="flex gap-4 items-center flex-col sm:flex-row">
          <a
            className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:w-auto"
            href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
            target="_blank"
            rel="noopener noreferrer"
          >
            <Image
              className="dark:invert"
              src="/vercel.svg"
              alt="Vercel logomark"
              width={20}
              height={20}
            />
            Start Game
          </a>
          <button
            className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 w-full sm:w-auto md:w-[158px]"
            onClick={resetGame}
          >
            Reset Game</button>
           <button
            onClick={advanceTurn}
            className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:w-auto">Next Turn</button>
        </div>
      </main>
    </div>
  );
}
