import Link from "next/link";
import { login } from "./actions";

export default async function LoginPage({
  searchParams,
}: {
  searchParams?: Promise<{ [key: string]: string | string[] | undefined }>
}) {
  const params = await searchParams;
  const error = params?.error as string | undefined;
  const message = params?.message as string | undefined;

  return (
    <>
      {/* TopAppBar */}
      <header className="bg-[#131313] flex justify-between items-center px-8 h-[72px] w-full docked full-width top-0 z-50">
        <div className="flex items-center gap-4">
          <Link href="/">
            <span className="material-symbols-outlined text-[#CCFF00] cursor-pointer hover:opacity-80 transition-opacity active:scale-95 duration-200">
              arrow_back
            </span>
          </Link>
          <span className="text-[#CCFF00] font-bold tracking-tight text-2xl uppercase font-headline">
            VivaFit 50+
          </span>
        </div>
      </header>

      <main className="grow flex items-center justify-center px-6 py-12 bg-hero-pattern relative min-h-[calc(100vh-72px)]">
        {/* Login Card */}
        <div className="w-full max-w-[500px] bg-surface-container-low rounded-[32px] p-8 md:p-12 shadow-2xl backdrop-blur-md bg-opacity-90 border border-white/5">
          <div className="mb-10 text-left">
            <h1 className="font-headline font-bold text-3xl md:text-4xl text-primary leading-tight mb-3">
              Bem-vindo ao VivaFit 50+
            </h1>
            <p className="font-body text-lg text-on-surface-variant font-medium">
              Entre na sua conta para continuar
            </p>
          </div>

          {error && (
            <div className="mb-6 p-4 bg-error-container/20 border border-error/50 rounded-xl text-error text-sm font-bold">
              {error}
            </div>
          )}

          {message && (
            <div className="mb-6 p-4 bg-[#c3f400]/10 border border-[#c3f400]/30 rounded-xl text-[#c3f400] text-sm font-bold">
              {message}
            </div>
          )}

          <form className="space-y-8" action={login}>
            {/* Email Input */}
            <div className="space-y-3">
              <label
                className="block font-headline font-semibold text-sm uppercase tracking-wider text-on-surface-variant ml-1"
                htmlFor="email"
              >
                E-mail
              </label>
              <div className="relative">
                <input
                  className="w-full h-16 bg-surface-container-high border-none rounded-xl px-6 text-lg text-primary focus:ring-2 focus:ring-primary-fixed transition-all placeholder:text-white/20 font-medium"
                  id="email"
                  name="email"
                  placeholder="seu@email.com"
                  type="email"
                  required
                />
                <span className="material-symbols-outlined absolute right-5 top-1/2 -translate-y-1/2 text-white/40">
                  mail
                </span>
              </div>
            </div>
            {/* Password Input */}
            <div className="space-y-3">
              <div className="flex justify-between items-center px-1">
                <label
                  className="font-headline font-semibold text-sm uppercase tracking-wider text-on-surface-variant"
                  htmlFor="password"
                >
                  Senha
                </label>
                <a
                  className="text-primary-fixed text-sm font-semibold hover:underline"
                  href="#"
                >
                  Esqueceu a senha?
                </a>
              </div>
              <div className="relative">
                <input
                  className="w-full h-16 bg-surface-container-high border-none rounded-xl px-6 text-lg text-primary focus:ring-2 focus:ring-primary-fixed transition-all placeholder:text-white/20 font-medium"
                  id="password"
                  name="password"
                  placeholder="••••••••"
                  type="password"
                  required
                />
                <span className="material-symbols-outlined absolute right-5 top-1/2 -translate-y-1/2 text-white/40">
                  lock
                </span>
              </div>
            </div>
            {/* Login Button */}
            <button
              className="w-full h-[64px] bg-[#CCFF00] text-[#161E00] font-headline font-extrabold text-xl rounded-xl shadow-[0_0_30px_rgba(204,255,0,0.15)] hover:opacity-90 active:scale-[0.98] transition-all flex items-center justify-center gap-3"
              type="submit"
            >
              <span>ENTRAR</span>
              <span className="material-symbols-outlined font-bold">login</span>
            </button>
          </form>

          {/* Desktop Register Prompt */}
          <div className="mt-8 text-center hidden md:block">
            <p className="text-on-surface-variant font-medium">
              Não tem uma conta?{" "}
              <Link
                href="/signup"
                className="text-[#CCFF00] font-bold hover:underline ml-1"
              >
                Cadastre-se agora
              </Link>
            </p>
          </div>
        </div>
      </main>

      {/* BottomNavBar for Mobile */}
      <nav className="fixed bottom-0 left-0 w-full z-50 flex justify-around items-center px-10 pb-8 pt-4 bg-[#131313] rounded-t-3xl shadow-[0_-4px_20px_rgba(204,255,0,0.05)] md:hidden">
        <Link
          href="/login"
          className="flex flex-col items-center justify-center bg-[#CCFF00] text-[#161E00] rounded-2xl px-8 py-2 active:scale-98 transition-transform duration-200"
        >
          <span
            className="material-symbols-outlined"
            style={{ fontVariationSettings: "'FILL' 1" }}
          >
            login
          </span>
          <span className="font-headline font-semibold text-xs mt-1">Login</span>
        </Link>
        <Link
          href="/signup"
          className="flex flex-col items-center justify-center text-white/70 px-8 py-2 hover:text-white transition-colors"
        >
          <span className="material-symbols-outlined">person_add</span>
          <span className="font-headline font-semibold text-xs mt-1">
            Sign Up
          </span>
        </Link>
      </nav>
    </>
  );
}
