"use client";

import { useState } from "react";

interface QuizQuestionProps {
  question: {
    id: string;
    text: string;
    options: string[];
    correctAnswer?: number;
  };
  onAnswer: (selectedIndex: number, isCorrect: boolean) => void;
}

export default function QuizQuestion({ question, onAnswer }: QuizQuestionProps) {
  const [selectedOption, setSelectedOption] = useState<number | null>(null);
  const [submitted, setSubmitted] = useState(false);

  const handleSubmit = () => {
    if (selectedOption === null) return;

    const isCorrect = question.correctAnswer !== undefined
      ? selectedOption === question.correctAnswer
      : false;

    setSubmitted(true);
    onAnswer(selectedOption, isCorrect);
  };

  const getOptionStyle = (index: number) => {
    if (!submitted) {
      return selectedOption === index
        ? "bg-yellow-500/20 border-yellow-500"
        : "bg-gray-800 border-gray-700 hover:border-gray-600";
    }

    if (question.correctAnswer !== undefined) {
      if (index === question.correctAnswer) {
        return "bg-green-500/20 border-green-500";
      }
      if (index === selectedOption && selectedOption !== question.correctAnswer) {
        return "bg-red-500/20 border-red-500";
      }
    }

    return "bg-gray-800 border-gray-700 opacity-50";
  };

  const getOptionIcon = (index: number) => {
    if (!submitted) return null;

    if (question.correctAnswer !== undefined) {
      if (index === question.correctAnswer) {
        return (
          <svg className="w-6 h-6 text-green-400" fill="currentColor" viewBox="0 0 20 20">
            <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clipRule="evenodd" />
          </svg>
        );
      }
      if (index === selectedOption && selectedOption !== question.correctAnswer) {
        return (
          <svg className="w-6 h-6 text-red-400" fill="currentColor" viewBox="0 0 20 20">
            <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
          </svg>
        );
      }
    }
    return null;
  };

  return (
    <div className="bg-gray-800 border border-gray-700 rounded-xl p-8">
      <div className="mb-6">
        <h3 className="text-2xl font-bold text-white mb-2">{question.text}</h3>
        <p className="text-gray-400 text-sm">Select one answer</p>
      </div>

      <div className="space-y-3 mb-6">
        {question.options.map((option, index) => (
          <button
            key={index}
            onClick={() => !submitted && setSelectedOption(index)}
            disabled={submitted}
            className={`w-full flex items-center justify-between p-4 border-2 rounded-lg transition ${getOptionStyle(index)} ${
              !submitted ? "cursor-pointer" : "cursor-not-allowed"
            }`}
          >
            <div className="flex items-center gap-3">
              <div className={`w-6 h-6 rounded-full border-2 flex items-center justify-center ${
                selectedOption === index && !submitted
                  ? "border-yellow-500 bg-yellow-500"
                  : "border-gray-600"
              }`}>
                {selectedOption === index && !submitted && (
                  <div className="w-3 h-3 rounded-full bg-white"></div>
                )}
              </div>
              <span className="text-white font-medium">{option}</span>
            </div>
            {getOptionIcon(index)}
          </button>
        ))}
      </div>

      {submitted ? (
        <div className={`p-4 rounded-lg border-2 ${
          question.correctAnswer !== undefined && selectedOption === question.correctAnswer
            ? "bg-green-500/20 border-green-500"
            : "bg-red-500/20 border-red-500"
        }`}>
          <p className={`font-semibold ${
            question.correctAnswer !== undefined && selectedOption === question.correctAnswer
              ? "text-green-400"
              : "text-red-400"
          }`}>
            {question.correctAnswer !== undefined && selectedOption === question.correctAnswer
              ? "Correct! Well done!"
              : "Incorrect. Try again next time!"}
          </p>
        </div>
      ) : (
        <button
          onClick={handleSubmit}
          disabled={selectedOption === null}
          className="w-full bg-gradient-to-r from-yellow-500 to-orange-500 text-white font-semibold py-3 rounded-lg hover:from-yellow-600 hover:to-orange-600 transition transform hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed disabled:transform-none"
        >
          Submit Answer
        </button>
      )}
    </div>
  );
}
