"use client";

import Link from "next/link";
import { useActionState } from "react";
import { ArrowLeft, Loader2, MailCheck } from "lucide-react";

import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { cn } from "@/lib/utils";
import { requestPasswordResetAction } from "@/lib/appwrite/password-reset-actions";
import { initialAuthState } from "@/lib/appwrite/auth-types";

export function ForgotPasswordForm1({ className, ...props }: React.ComponentProps<"div">) {
  const [state, formAction, isPending] = useActionState(requestPasswordResetAction, initialAuthState);

  return (
    <div className={cn("flex flex-col gap-6", className)} {...props}>
      <Card>
        <CardHeader className="text-center">
          <CardTitle className="text-xl">Şifremi unuttum</CardTitle>
          <CardDescription>
            Email adresinizi girin, sıfırlama bağlantısı gönderelim.
          </CardDescription>
        </CardHeader>
        <CardContent>
          {state.ok ? (
            <div className="flex flex-col items-center gap-3 py-4 text-center">
              <div className="bg-primary/10 text-primary flex size-12 items-center justify-center rounded-full">
                <MailCheck className="size-6" />
              </div>
              <p className="text-sm">
                Sıfırlama kodunuz e-posta adresinize gönderildi. Kodu girerek şifrenizi yenileyebilirsiniz.
              </p>
              <Link
                href="/sign-in"
                className="text-muted-foreground hover:text-foreground mt-2 flex items-center gap-1 text-sm underline-offset-4 hover:underline"
              >
                <ArrowLeft className="size-3.5" />
                Giriş sayfasına dön
              </Link>
            </div>
          ) : (
            <form action={formAction} className="flex flex-col gap-4">
              <div className="grid gap-3">
                <Label htmlFor="email">Email</Label>
                <Input
                  id="email"
                  name="email"
                  type="email"
                  placeholder="ornek@firma.com"
                  autoComplete="email"
                  required
                />
              </div>

              {state.error && (
                <p className="text-destructive text-sm text-center" role="alert">
                  {state.error}
                </p>
              )}

              <Button type="submit" className="w-full" disabled={isPending}>
                {isPending ? (
                  <>
                    <Loader2 className="size-4 animate-spin" />
                    Gönderiliyor...
                  </>
                ) : (
                  "Sıfırlama bağlantısı gönder"
                )}
              </Button>

              <Link
                href="/sign-in"
                className="text-muted-foreground hover:text-foreground flex items-center justify-center gap-1 text-sm underline-offset-4 hover:underline"
              >
                <ArrowLeft className="size-3.5" />
                Giriş sayfasına dön
              </Link>
            </form>
          )}
        </CardContent>
      </Card>
    </div>
  );
}
