import { cn } from '@/lib/utils';

export function scorePassword(pw: string): number {
    let s = 0;
    if (pw.length >= 8) s++;
    if (pw.length >= 12) s++;
    if (/[A-Z]/.test(pw) && /[a-z]/.test(pw)) s++;
    if (/\d/.test(pw) && /[^A-Za-z0-9]/.test(pw)) s++;
    return s;
}

const LABELS = ['Too short', 'Weak', 'Okay', 'Good', 'Strong'];

export default function PasswordStrength({ value }: { value: string }) {
    if (!value) return null;
    const s = scorePassword(value);
    return (
        <div className="space-y-1" aria-live="polite">
            <div className="flex gap-1">
                {[1, 2, 3, 4].map((i) => (
                    <span key={i} className={cn('h-1 flex-1 rounded-full bg-muted transition-colors', s >= i && (s <= 1 ? 'bg-destructive' : s === 2 ? 'bg-amber-500' : 'bg-primary'))} />
                ))}
            </div>
            <p className="text-xs text-muted-foreground">{LABELS[s]}</p>
        </div>
    );
}
