'use client';

import { motion, useReducedMotion } from 'framer-motion';

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

// UI-only hint. The server schema does not reject common passwords; this list only guides the user.
const COMMON = new Set([
  'password1', 'password12', 'password123', 'password1234', 'qwerty1234', 'qwerty12345', '1234567890', 'abc1234567',
  'iloveyou12', 'welcome123', 'letmein123', 'admin12345', 'passw0rd12', 'football12', 'baseball12', 'sunshine12',
]);

const segmentColor = ['bg-danger', 'bg-warning', 'bg-info', 'bg-success'] as const;
const labels = ['Too weak', 'Weak', 'Fair', 'Good', 'Strong'] as const;
const labelColor = ['text-subtle-foreground', 'text-danger', 'text-warning', 'text-info', 'text-success'] as const;

export function PasswordStrength({ password }: { password: string }) {
  const reduce = useReducedMotion();
  const base = passwordChecks(password);
  const shortLabels: Record<string, string> = { length: '10 characters', letter: 'A letter', number: 'A number' };
  const checks = [
    ...base.map((check) => ({ id: check.id, label: shortLabels[check.id] ?? check.label, met: check.met })),
    { id: 'common', label: 'Not a common password', met: password.length >= 10 && !COMMON.has(password.toLowerCase()) },
  ];
  const score = password.length === 0 ? 0 : checks.filter((check) => check.met).length;

  return (
    <div className="grid gap-3">
      <div className="flex items-center gap-3">
        <div
          className="grid flex-1 grid-cols-4 gap-1.5"
          role="meter"
          aria-label="Password strength"
          aria-valuemin={0}
          aria-valuemax={4}
          aria-valuenow={score}
          aria-valuetext={labels[score]}
        >
          {segmentColor.map((color, index) => (
            <span key={color} className="h-1.5 overflow-hidden rounded-pill bg-border">
              <span
                className={cn('block h-full rounded-pill transition-[width,background-color] duration-280 ease-brand', segmentColor[Math.max(score - 1, 0)])}
                style={{ width: index < score ? '100%' : '0%' }}
              />
            </span>
          ))}
        </div>
        <span className={cn('w-16 text-right text-caption font-semibold', labelColor[score])}>{password.length === 0 ? '' : labels[score]}</span>
      </div>
      <ul className="grid grid-cols-2 gap-x-4 gap-y-1.5">
        {checks.map((check) => (
          <li key={check.id} className={cn('flex items-center gap-2 text-caption transition-colors', check.met ? 'text-foreground' : 'text-muted-foreground')}>
            <span
              className={cn(
                'grid h-4 w-4 shrink-0 place-items-center rounded-full border transition-colors duration-180',
                check.met ? 'border-success bg-success' : 'border-border-strong',
              )}
              aria-hidden="true"
            >
              {check.met ? (
                <svg viewBox="0 0 12 12" className="h-2.5 w-2.5" fill="none">
                  <motion.path
                    d="M2.5 6.2l2.3 2.3 4.7-5"
                    className="stroke-primary-foreground"
                    strokeWidth="1.8"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    initial={reduce ? false : { pathLength: 0 }}
                    animate={{ pathLength: 1 }}
                    transition={{ duration: 0.25 }}
                  />
                </svg>
              ) : null}
            </span>
            <span>
              {check.label}
              <span className="sr-only">{check.met ? ' (met)' : ' (not met yet)'}</span>
            </span>
          </li>
        ))}
      </ul>
    </div>
  );
}
