"use client";

import React, { useState } from "react";
import { Button } from "@/components/ui/button";
import { useTranslations } from "next-intl";
import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@/components/ui/input-otp";
import { OTPFormProps } from "@/types/auth";
import { useOTPVerification } from "@/hooks/use-auth";

export const OTPForm: React.FC<OTPFormProps> = ({
  loginCredentials,
  onSuccess,
  onError,
  onResend,
  className,
}) => {
  const t = useTranslations("LandingPage");
  const { verifyOTP, loading } = useOTPVerification();
  const [otpValue, setOtpValue] = useState("");

  const handleTypeOTP = (event: React.KeyboardEvent<HTMLInputElement>) => {
    const { key } = event;
    const target = event.currentTarget;

    if (key === "Enter") {
      event.preventDefault();

      const inputs = Array.from(target.form?.querySelectorAll("input") || []);
      const currentIndex = inputs.indexOf(target);
      const nextInput = inputs[currentIndex + 1] as HTMLElement | undefined;

      if (nextInput) {
        nextInput.focus();
      }
    }
  };

  const handleOTPChange = (value: string) => {
    setOtpValue(value);
  };

  const handleSubmit = async () => {
    if (otpValue.length !== 6) {
      onError?.("Please enter a complete 6-digit OTP");
      return;
    }

    if (!loginCredentials?.username) {
      onError?.("Username not found. Please try logging in again.");
      return;
    }

    try {
      const isValid = await verifyOTP(loginCredentials.username, otpValue);
      if (isValid) {
        onSuccess?.();
      } else {
        onError?.("Invalid OTP code");
      }
    } catch (error: any) {
      onError?.(error.message || "OTP verification failed");
    }
  };

  // const handleResend = () => {
  //   onResend?.();
  // };

  return (
    <div className={className}>
      <div className="space-y-6">
        {/* Header */}
        <div className="text-left space-y-2">
          <h1 className="font-semibold text-3xl text-left">
            {t("pleaseEnterOtp", { defaultValue: "Please Enter OTP" })}
          </h1>
          <p className="text-default-500 text-base">
            Enter the 6-digit code sent to your email address.
          </p>
        </div>

        {/* OTP Input */}
        <div className="flex justify-center">
          <InputOTP
            maxLength={6}
            value={otpValue}
            onChange={handleOTPChange}
            disabled={loading}
            className="gap-2"
          >
            <InputOTPGroup>
              <InputOTPSlot
                index={0}
                onKeyDown={handleTypeOTP}
                className="w-12 h-12 text-lg"
              />
              <InputOTPSlot
                index={1}
                onKeyDown={handleTypeOTP}
                className="w-12 h-12 text-lg"
              />
            </InputOTPGroup>
            <InputOTPSeparator />
            <InputOTPGroup>
              <InputOTPSlot
                index={2}
                onKeyDown={handleTypeOTP}
                className="w-12 h-12 text-lg"
              />
              <InputOTPSlot
                index={3}
                onKeyDown={handleTypeOTP}
                className="w-12 h-12 text-lg"
              />
            </InputOTPGroup>
            <InputOTPSeparator />
            <InputOTPGroup>
              <InputOTPSlot
                index={4}
                onKeyDown={handleTypeOTP}
                className="w-12 h-12 text-lg"
              />
              <InputOTPSlot
                index={5}
                onKeyDown={handleTypeOTP}
                className="w-12 h-12 text-lg"
              />
            </InputOTPGroup>
          </InputOTP>
        </div>

        {/* Resend OTP */}
        <div className="text-center">
          <button
            type="button"
            onClick={onResend}
            disabled={loading}
            className="text-sm text-blue-600 hover:text-blue-800 underline disabled:opacity-50 disabled:cursor-not-allowed"
          >
            Didn't receive the code? Resend
          </button>
        </div>

        {/* Submit Button */}
        <Button
          type="button"
          fullWidth
          onClick={handleSubmit}
          disabled={otpValue.length !== 6 || loading}
          className="bg-red-500 hover:bg-red-600"
        >
          {loading ? (
            <div className="flex items-center gap-2">
              <div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin" />
              Verifying...
            </div>
          ) : (
            t("signIn", { defaultValue: "Sign In" })
          )}
        </Button>
      </div>
    </div>
  );
};
