"use client"

import { useRouter } from "next/navigation"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogDescription,
} from "@/components/ui/dialog"
import { Check, Eye, EyeOff } from "lucide-react"

export default function ForgotResetPage() {
  const router = useRouter()
  const [password, setPassword] = useState("")
  const [confirm, setConfirm] = useState("")
  const [showPassword, setShowPassword] = useState(false)
  const [showConfirm, setShowConfirm] = useState(false)
  const [showSuccess, setShowSuccess] = useState(false)

  const meetsLength = password.length >= 8 && password.length <= 20
  const hasUpper = /[A-Z]/.test(password)
  const hasNumber = /[0-9]/.test(password)
  const hasSpecial = /[^A-Za-z0-9]/.test(password)
  const passwordsMatch = password === confirm && password.length > 0
  const allValid = meetsLength && hasUpper && hasNumber && hasSpecial && passwordsMatch

  const PasswordRequirement = ({ met, label }: { met: boolean; label: string }) => (
    <div className="flex items-center gap-3">
      <div className={`flex items-center justify-center w-5 h-5 rounded-full shrink-0 text-sm font-semibold ${met ? 'bg-success/10 text-success' : 'bg-muted text-muted-foreground'}`}>
        {met ? <Check className="size-4" /> : <span>○</span>}
      </div>
      <span className={`text-sm ${met ? 'text-foreground font-medium' : 'text-muted-foreground'}`}>{label}</span>
    </div>
  )

  return (
    <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-background to-background/95 p-4">
      <div className="w-full max-w-xl rounded-xl bg-card p-8 shadow-lg border border-border">
        <div className="mb-8 text-center">
          <h1 className="text-2xl font-bold text-foreground">Create a new password</h1>
          <p className="mt-2 text-sm text-muted-foreground">Choose a strong password to secure your account.</p>
        </div>

        <div className="space-y-6">
          <div>
            <label htmlFor="password" className="block text-sm font-semibold text-foreground mb-2">New password</label>
            <div className="relative">
              <Input
                id="password"
                type={showPassword ? 'text' : 'password'}
                value={password}
                onChange={(e) => setPassword((e.target as HTMLInputElement).value)}
                className="h-11 pr-11"
                placeholder="Enter your new password"
              />
              <button
                type="button"
                onClick={() => setShowPassword((s) => !s)}
                className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors"
              >
                {showPassword ? <EyeOff className="size-5" /> : <Eye className="size-5" />}
              </button>
            </div>
          </div>

          <div>
            <label htmlFor="confirm" className="block text-sm font-semibold text-foreground mb-2">Confirm password</label>
            <div className="relative">
              <Input
                id="confirm"
                type={showConfirm ? 'text' : 'password'}
                value={confirm}
                onChange={(e) => setConfirm((e.target as HTMLInputElement).value)}
                className="h-11 pr-11"
                placeholder="Confirm your new password"
              />
              <button
                type="button"
                onClick={() => setShowConfirm((s) => !s)}
                className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground transition-colors"
              >
                {showConfirm ? <EyeOff className="size-5" /> : <Eye className="size-5" />}
              </button>
            </div>
          </div>

          <div className="bg-muted/50 rounded-lg p-4 space-y-3">
            <p className="text-sm font-semibold text-foreground">Password requirements:</p>
            <PasswordRequirement met={meetsLength} label="Between 8 and 20 characters" />
            <PasswordRequirement met={hasUpper} label="At least 1 uppercase letter" />
            <PasswordRequirement met={hasNumber} label="At least 1 number" />
            <PasswordRequirement met={hasSpecial} label="At least 1 special character" />
          </div>

          {!passwordsMatch && confirm.length > 0 && (
            <p className="text-sm text-error font-medium">Passwords don't match.</p>
          )}

          <div className="flex gap-3">
            <Button
              variant="outline"
              onClick={() => router.push('/login')}
              className="flex-1 h-12 rounded-full border-input text-foreground hover:bg-foreground/5"
            >
              Back
            </Button>
            <Button
              disabled={!allValid}
              onClick={() => setShowSuccess(true)}
              className="flex-1 h-12 rounded-full bg-foreground text-background hover:bg-foreground/90"
            >
              Change password
            </Button>
          </div>
        </div>

        <Dialog open={showSuccess} onOpenChange={setShowSuccess}>
          <DialogContent className="w-full max-w-lg sm:max-w-lg">
            <DialogHeader className="text-center">
              <div className="flex flex-col items-center gap-4">
                <div className="flex items-center justify-center w-12 h-12 rounded-full bg-success/10 text-success">
                  <Check className="size-6" />
                </div>
                <div>
                  <DialogTitle className="text-xl font-bold">Password changed successfully</DialogTitle>
                  <DialogDescription className="mt-2">Your password has been updated. You can now sign in with your new password.</DialogDescription>
                </div>
              </div>
            </DialogHeader>
            <Button onClick={() => router.push('/login')} className="w-4/5 mx-auto h-12 mt-6 rounded-full bg-foreground text-background hover:bg-foreground/90">
              Return to sign in
            </Button>
          </DialogContent>
        </Dialog>
      </div>
    </div>
  )
}
