"use client";

import Link from "next/link";
import { useActionState } from "react";
import { loginUser } from "@/lib/actions/auth";

export function LoginForm() {
  const [state, formAction, isPending] = useActionState(loginUser, undefined);

  return (
    <form action={formAction} className="mt-8 flex flex-col gap-4">
      {state?.error && (
        <p className="rounded bg-terracotta/10 px-4 py-2 text-sm text-terracotta">
          {state.error}
        </p>
      )}

      <label className="flex flex-col gap-1">
        <span className="text-sm font-medium text-charcoal">Email</span>
        <input
          type="email"
          name="email"
          required
          className="rounded border border-charcoal/20 bg-warm-white px-4 py-2 text-charcoal"
        />
      </label>

      <label className="flex flex-col gap-1">
        <span className="text-sm font-medium text-charcoal">Password</span>
        <input
          type="password"
          name="password"
          required
          className="rounded border border-charcoal/20 bg-warm-white px-4 py-2 text-charcoal"
        />
      </label>

      <Link
        href="/forgot-password"
        className="text-sm text-terracotta hover:underline"
      >
        Forgot password?
      </Link>

      <button
        type="submit"
        disabled={isPending}
        className="mt-2 rounded-full bg-terracotta px-6 py-2 font-medium text-white hover:bg-terracotta/90 disabled:opacity-60"
      >
        {isPending ? "Logging in..." : "Log In"}
      </button>
    </form>
  );
}
