import React from 'react';
import { motion } from 'motion/react';
import { getQuestionRubric } from '../../data/questionnaire';
import { Leader, Aspect, Question } from '../../types';

interface AspectWizardProps {
  leaders: Leader[];
  leaderId: string;
  currentStep: number;
  scores: Record<number, number>;
  setScores: React.Dispatch<React.SetStateAction<Record<number, number>>>;
  aspects: Aspect[];
  questions: Question[];
}

export default function AspectWizard({
  leaders,
  leaderId,
  currentStep,
  scores,
  setScores,
  aspects,
  questions
}: AspectWizardProps) {
  const currentAspect = aspects[currentStep - 1] || { id: 0, title: '', description: '' };
  const activeQuestions = questions.filter(q => q.aspectId === currentAspect.id);
  const activeLeader = leaders.find(l => l.id === leaderId);

  return (
    <motion.div
      key={`step-aspect-${currentStep}-${leaderId}`}
      initial={{ opacity: 0, x: 15 }}
      animate={{ opacity: 1, x: 0 }}
      exit={{ opacity: 0, x: -15 }}
      transition={{ duration: 0.18 }}
    >
      <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-1.5 mb-4 border-b border-slate-100 pb-3">
        <span className="text-[10px] font-bold text-blue-700 bg-blue-50 border border-blue-100 px-3 py-1 rounded-md uppercase tracking-widest">
          ASPEK EVALUASI {currentStep} DARI 6 (BAGIAN B)
        </span>
        <div className="text-[11px] font-semibold text-slate-400">
          Skala: 1 (Sangat Tidak Setuju) s.d 5 (Sangat Setuju)
        </div>
      </div>

      {/* Aspect Box Details */}
      <div className="mb-6 bg-slate-50 p-4.5 rounded-xl border border-slate-200/50">
        <h3 className="text-base font-bold text-slate-900 font-sans">
          {currentAspect.title}
        </h3>
        <p className="text-xs mt-1 text-slate-600 leading-relaxed">
          {currentAspect.description}
        </p>
        
        {/* Active Context Box */}
        <div className="mt-3.5 pt-2.5 border-t border-slate-200/50 flex items-center gap-1.5 text-xs font-bold text-blue-700">
          <span>Sedang Menilai Kinerja:</span>
          <span className="bg-blue-600 text-white px-2.5 py-0.5 rounded uppercase tracking-widest text-[10px] font-extrabold shadow-3xs">
            {activeLeader?.position}
          </span>
        </div>
      </div>

      {/* Question List */}
      <div className="space-y-4">
        {activeQuestions.map((q) => {
          const activeScoreVal = scores[q.id];
          return (
            <div key={q.id} className="p-4 rounded-xl border border-slate-100 bg-white shadow-3xs hover:border-slate-200 transition-colors">
              <div className="flex items-start gap-3.5 mb-4">
                <span className="bg-slate-100 text-slate-800 h-7 w-7 shrink-0 rounded-full flex items-center justify-center text-xs font-bold font-mono border border-slate-200">
                  {q.id}
                </span>
                <p className="text-xs sm:text-sm font-bold text-slate-800 leading-relaxed mt-1">
                  {q.text}
                </p>
              </div>

              {/* Custom 5-Points Scale Buttons */}
              <div className="grid grid-cols-5 gap-1.5 sm:gap-3">
                {[1, 2, 3, 4, 5].map((val) => {
                  const isSelected = activeScoreVal === val;
                  return (
                    <button
                      type="button"
                      key={val}
                      onClick={() => setScores(p => ({ ...p, [q.id]: val }))}
                      className={`relative group flex flex-col items-center justify-center py-2.5 px-1 rounded-xl border transition-all cursor-pointer ${
                        isSelected
                          ? 'bg-blue-600 border-blue-600 text-white font-black shadow-md scale-[1.02] ring-1 ring-blue-400'
                          : 'border-slate-200 bg-slate-50/50 text-slate-500 hover:bg-slate-100 hover:border-slate-300'
                      }`}
                      title={getQuestionRubric(q.id, val)}
                    >
                      <span className="font-extrabold text-sm sm:text-base font-mono">
                        {val}
                      </span>

                      {/* Custom floating tooltip on hover */}
                      <span className="absolute bottom-full left-1/2 -translate-x-1/2 mb-2.5 hidden group-hover:flex flex-col items-center bg-slate-900 text-white text-xs font-semibold py-2.5 px-3.5 rounded-xl w-64 text-center shadow-lg pointer-events-none z-50">
                        <span>{getQuestionRubric(q.id, val)}</span>
                        {/* Little arrow indicator */}
                        <span className="absolute top-full left-1/2 -translate-x-1/2 border-4 border-transparent border-t-slate-900" />
                      </span>

                      <span className={`text-[8px] font-extrabold leading-tight mt-1 text-center px-0.5 ${
                        isSelected ? 'text-white' : 'text-slate-400'
                      }`}>
                        <span className="hidden sm:inline">
                          {val === 1 && 'Sangat Tidak Setuju'}
                          {val === 2 && 'Tidak Setuju'}
                          {val === 3 && 'Cukup'}
                          {val === 4 && 'Setuju'}
                          {val === 5 && 'Sangat Setuju'}
                        </span>
                        <span className="inline sm:hidden font-mono text-[7px] tracking-tighter">
                          {val === 1 && 'STS'}
                          {val === 2 && 'TS'}
                          {val === 3 && 'C'}
                          {val === 4 && 'S'}
                          {val === 5 && 'SS'}
                        </span>
                      </span>
                    </button>
                  );
                })}
              </div>

              {/* Dynamic Rubric Description for Mobile (Option A) */}
              {activeScoreVal && (
                <motion.div
                  key={`rubric-${q.id}-${activeScoreVal}`}
                  initial={{ opacity: 0, y: -4 }}
                  animate={{ opacity: 1, y: 0 }}
                  transition={{ duration: 0.2 }}
                  className="block sm:hidden mt-2.5 bg-blue-50/70 border border-blue-100/70 p-3 rounded-xl text-[10.5px] text-blue-800 leading-relaxed font-semibold transition-all shadow-2xs"
                >
                  <span>{getQuestionRubric(q.id, activeScoreVal)}</span>
                </motion.div>
              )}
            </div>
          );
        })}
      </div>
    </motion.div>
  );
}
