"use client"

import { useEffect, useState } from "react"
import { useRouter } from "next/navigation"
import { GatoMascota } from "@/components/child/gato-mascota"
import AudioUnlock from "@/components/child/audio-unlock"

const SEBASTIAN_KEY = "edueasy_child_sebastian"

const activities = [
  { id: "matematicas", label: "Matemáticas", icon: "➕", description: "Sumas, restas, multiplicación, división, fracciones" },
  { id: "lectura", label: "Lectura", icon: "📖", description: "Textos informativos y comprensión" },
  { id: "historia", label: "Historia argentina", icon: "🇦🇷", description: "Personajes, fechas y eventos" },
  { id: "geografia", label: "Geografía", icon: "🗺️", description: "Provincias, ríos y regiones" },
  { id: "banderas", label: "Banderas", icon: "🚩", description: "Provincias y símbolos" },
  { id: "logros", label: "Mis logros", icon: "⭐", color: "bg-warning" },
  { id: "pairing", label: "iPad nuevo", icon: "🔗", color: "bg-secondary" },
]

export default function SebastianHome() {
  const router = useRouter()
  const [audioReady, setAudioReady] = useState(false)
  const [paired, setPaired] = useState(false)

  useEffect(() => {
    setPaired(!!localStorage.getItem(SEBASTIAN_KEY))
  }, [])

  const handleActivity = (id: string) => {
    if (id === "logros") {
      router.push("/child/logros?profile=sebastian")
    } else if (id === "pairing") {
      router.push("/child/pairing?profile=sebastian")
    } else {
      router.push(`/sebastian/${id}`)
    }
  }

  if (!audioReady) {
    return <AudioUnlock onUnlock={() => setAudioReady(true)} />
  }

  return (
    <div className="flex-1 flex flex-col p-4">
      <button
        onClick={() => router.push("/")}
        className="self-start flex items-center gap-2 bg-surface text-foreground border border-border rounded-xl px-4 py-2 text-base font-display font-semibold shadow-sm active:scale-95 transition-transform mb-4"
      >
        <span className="text-xl">←</span>
        <span>Cambiar perfil</span>
      </button>

      <div className="flex-1 flex flex-col items-center justify-center gap-8">
        <GatoMascota mood="happy" message="¡Hola Sebastián! ¿Qué querés explorar hoy?" />

      {!paired ? (
        <div className="text-center max-w-xs space-y-4">
          <p className="text-foreground/60">Todavía no estás conectado. Pedile a mamá que te empareje.</p>
          <button
            onClick={() => router.push("/child/pairing?profile=sebastian")}
            className="bg-primary text-primary-foreground touch-target-lg rounded-2xl px-8 py-4 text-xl font-display font-bold"
          >
            Ir a emparejar
          </button>
        </div>
      ) : (
        <div className="flex flex-col gap-4 w-full max-w-md">
          {activities.map((a) => (
            <button
              key={a.id}
              onClick={() => handleActivity(a.id)}
              className={`${a.color || "bg-accent"} text-primary-foreground touch-target-lg rounded-2xl flex items-center gap-4 px-6 py-5 text-xl font-display font-semibold shadow-md active:scale-95 transition-transform`}
            >
              <span className="text-2xl">{a.icon}</span>
              <div className="text-left">
                <div>{a.label}</div>
                {a.description && (
                  <div className="text-sm opacity-80">{a.description}</div>
                )}
              </div>
            </button>
          ))}
        </div>
      )}
      </div>
    </div>
  )
}
