"use client"

import { useState, useEffect, useCallback } from "react"
import Link from "next/link"

type Mode = "login" | "register"

function Logo() {
  return (
    <div className="flex items-center">
      {/* Red slashes */}
      <div className="flex gap-[3px] mr-2">
        {[0, 1, 2].map((i) => (
          <div key={i} className="w-[5px] h-[38px] bg-[#cc2222] -skew-x-12" />
        ))}
      </div>
      {/* HARIF box */}
      <div className="bg-[#852222] px-3 py-1 -skew-x-12 flex items-center h-[38px]">
        <span className="text-2xl font-black text-white italic tracking-tighter skew-x-12 inline-block leading-none">
          HARIF
        </span>
      </div>
      {/* SPORT text */}
      <span className="text-2xl font-black text-[#ff9800] italic tracking-tighter ml-1 leading-none">
        SPORT
      </span>
    </div>
  )
}

interface AuthModalProps {
  open: boolean
  defaultMode: Mode
  onClose: () => void
}

export function AuthModal({ open, defaultMode, onClose }: AuthModalProps) {
  const [mode, setMode] = useState<Mode>(defaultMode)
  const [phone, setPhone] = useState("")
  const [password, setPassword] = useState("")
  const [repeatPassword, setRepeatPassword] = useState("")
  const [ageConfirmed, setAgeConfirmed] = useState(false)

  // Sync mode if parent changes defaultMode while open
  useEffect(() => {
    if (open) setMode(defaultMode)
  }, [defaultMode, open])

  // Close on Escape
  const handleKey = useCallback(
    (e: KeyboardEvent) => {
      if (e.key === "Escape") onClose()
    },
    [onClose]
  )
  useEffect(() => {
    if (open) window.addEventListener("keydown", handleKey)
    return () => window.removeEventListener("keydown", handleKey)
  }, [open, handleKey])

  if (!open) return null

  return (
    <div
      className="fixed inset-0 z-[9999] flex items-center justify-center bg-black/70"
      onClick={onClose}
    >
      <div
        className="relative w-[420px] bg-[#3a3a3a] shadow-2xl animate-fade-in"
        onClick={(e) => e.stopPropagation()}
      >
        {/* Close button */}
        <button
          onClick={onClose}
          className="absolute top-2 right-3 text-white/60 hover:text-white text-xl leading-none transition-colors z-10"
          aria-label="Close"
        >
          ×
        </button>

        {/* Logo */}
        <div className="flex items-center justify-center py-5 border-b border-white/10 bg-[#2e2e2e]">
          <Logo />
        </div>

        {/* Title */}
        <div className="text-center py-4">
          <h2 className="text-sm font-black text-white uppercase tracking-widest">
            {mode === "login" ? "LOGIN" : "REGISTER"}
          </h2>
        </div>

        {/* Form */}
        <div className="px-8 pb-6 space-y-4">
          {/* Phone Number */}
          <div>
            <label className="block text-[11px] font-semibold text-white/80 mb-1">
              Phone Number
            </label>
            <div className="flex">
              <span className="flex items-center justify-center bg-white text-[#333] text-[12px] font-bold px-3 border border-gray-300 whitespace-nowrap select-none">
                ET +251
              </span>
              <input
                type="tel"
                value={phone}
                onChange={(e) => setPhone(e.target.value)}
                className="flex-1 bg-white border border-l-0 border-gray-300 px-3 py-2 text-sm text-[#333] placeholder:text-gray-400 focus:outline-none focus:border-[#ff9800]"
              />
            </div>
          </div>

          {/* Password */}
          <div>
            <label className="block text-[11px] font-semibold text-white/80 mb-1">
              Password
            </label>
            <input
              type="password"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              placeholder="Password"
              className="w-full bg-white border border-gray-300 px-3 py-2 text-sm text-[#333] placeholder:text-gray-400 focus:outline-none focus:border-[#ff9800]"
            />
          </div>

          {/* Repeat Password (register only) */}
          {mode === "register" && (
            <div>
              <label className="block text-[11px] font-semibold text-white/80 mb-1">
                Repeat Password
              </label>
              <input
                type="password"
                value={repeatPassword}
                onChange={(e) => setRepeatPassword(e.target.value)}
                placeholder="Repeat Password"
                className="w-full bg-white border border-gray-300 px-3 py-2 text-sm text-[#333] placeholder:text-gray-400 focus:outline-none focus:border-[#ff9800]"
              />
            </div>
          )}

          {/* Age confirmation (register only) */}
          {mode === "register" && (
            <label className="flex items-center gap-2 cursor-pointer">
              <input
                type="checkbox"
                checked={ageConfirmed}
                onChange={(e) => setAgeConfirmed(e.target.checked)}
                className="w-4 h-4 accent-[#ff9800]"
              />
              <span className="text-[12px] text-white/80">
                I confirm I&apos;m over 21 years old
              </span>
            </label>
          )}

          {/* Forgot password (login only) */}
          {mode === "login" && (
            <div className="text-right -mt-2">
              <Link
                href="/reset-password"
                className="text-[11px] text-white/50 hover:text-[#ff9800] transition-colors"
                onClick={onClose}
              >
                Forgot password?
              </Link>
            </div>
          )}

          {/* Submit button */}
          <button className="w-full bg-[#e6b800] hover:bg-[#ffcc00] text-black font-black py-3 uppercase text-sm tracking-widest transition-colors">
            {mode === "login" ? "LOGIN" : "REGISTER"}
          </button>

          {/* Support link */}
          <p className="text-center text-[12px] text-white/60">
            <Link
              href="/support"
              className="hover:text-white transition-colors"
              onClick={onClose}
            >
              Support
            </Link>
          </p>

          {/* Toggle mode */}
          <p className="text-center text-[11px] text-white/50">
            {mode === "login" ? (
              <>
                Don&apos;t have an account?{" "}
                <button
                  className="text-[#ff9800] hover:underline font-semibold"
                  onClick={() => setMode("register")}
                >
                  Register
                </button>
              </>
            ) : (
              <>
                Already have an account?{" "}
                <button
                  className="text-[#ff9800] hover:underline font-semibold"
                  onClick={() => setMode("login")}
                >
                  Login
                </button>
              </>
            )}
          </p>
        </div>
      </div>
    </div>
  )
}
