﻿import React from "react";
import { IQuestion } from "@/types";
import { Tag, Typography } from "antd";

const { Text } = Typography;

interface QuestionCardProps {
  index?: number;
  question: IQuestion;
}

const QuestionCard: React.FC<QuestionCardProps> = ({ index, question }) => {
  if (!question) return null;

  return (
    <div className="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden mb-4">
      {/* Header / Question Text */}
      <div className="p-5 border-b border-slate-100 bg-slate-50/50">
        {/* Question Metadata */}
        <div className="flex flex-wrap items-center gap-2 mb-3">
          <Tag
            color={
              question.difficulty === "Easy"
                ? "green"
                : question.difficulty === "Medium"
                ? "blue"
                : "red"
            }
            className="rounded-lg px-2.5 py-0.5 border-none font-medium"
          >
            {question.difficulty}
          </Tag>
          {question.subject && (
            <Tag
              color="purple"
              className="rounded-lg px-2.5 py-0.5 border-none font-medium"
            >
              {typeof question.subject === "object"
                ? (question.subject as any).subjectName
                : question.subject}
            </Tag>
          )}
          {question.language && (
            <Tag
              color="orange"
              className="rounded-lg px-2.5 py-0.5 border-none font-medium"
            >
              {question.language}
            </Tag>
          )}
          {question.topic && (
            <Tag
              color="cyan"
              className="rounded-lg px-2.5 py-0.5 border-none font-medium"
            >
              {question.topic}
            </Tag>
          )}
          <Tag
            color="blue"
            className="rounded-lg px-2.5 py-0.5 border-none font-medium"
          >
            {question.marks} Mark{question.marks > 1 ? "s" : ""}
          </Tag>
        </div>

        <div className="flex flex-col gap-4">
          <div className="flex items-start gap-3">
            <span className="text-lg font-bold text-brand-blue shrink-0">
              {index ? `${index}.` : "Q."}
            </span>
            <div className="text-md font-semibold text-slate-800 leading-relaxed whitespace-pre-wrap">
              {question.questionText}
            </div>
          </div>

          {question.questionImage && (
            <div className="ml-8 rounded-xl border border-slate-200 overflow-hidden bg-white max-w-2xl">
              <img
                src={question.questionImage}
                alt="Question"
                className="max-h-[300px] w-auto object-contain"
              />
            </div>
          )}
        </div>
      </div>

      {/* Options */}
      <div className="p-6">
        <h4 className="text-xs font-bold text-slate-400 uppercase tracking-widest mb-4">
          Answer Options
        </h4>
        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          {question.options.map((optionObj, idx) => {
            const isCorrect = question.correctAnswer === idx;
            return (
              <div
                key={`option-${idx}`}
                className={`group flex flex-col p-4 rounded-xl border transition-all duration-200 ${
                  isCorrect
                    ? "bg-green-50/50 border-green-500 shadow-sm ring-1 ring-green-500/20"
                    : "bg-white border-slate-200 hover:border-slate-300"
                }`}
              >
                <div className="flex items-center gap-3">
                  <div
                    className={`flex-shrink-0 w-8 h-8 rounded-full flex items-center justify-center text-sm font-bold border transition-colors ${
                      isCorrect
                        ? "bg-green-500 border-green-600 text-white"
                        : "bg-slate-100 border-slate-200 text-slate-500 group-hover:bg-slate-200"
                    }`}
                  >
                    {String.fromCharCode(65 + idx)}
                  </div>
                  <span
                    className={`text-md ${
                      isCorrect
                        ? "font-bold text-green-900"
                        : "font-medium text-slate-700"
                    }`}
                  >
                    {optionObj.option || (
                      <span className="italic text-slate-400">No text</span>
                    )}
                  </span>

                  {isCorrect && (
                    <div className="ml-auto text-green-600">
                      <svg
                        xmlns="http://www.w3.org/2000/svg"
                        className="h-6 w-6"
                        viewBox="0 0 20 20"
                        fill="currentColor"
                      >
                        <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>
                    </div>
                  )}
                </div>

                {optionObj.optionImage && (
                  <div className="mt-3 ml-11 rounded-lg border border-slate-100 overflow-hidden bg-white w-fit">
                    <img
                      src={optionObj.optionImage}
                      alt={`Option ${idx + 1}`}
                      className="max-h-[150px] w-auto object-contain p-1"
                    />
                  </div>
                )}
              </div>
            );
          })}
        </div>
      </div>

      {/* Explanation */}
      {(question.explanation || question.explanationImage) && (
        <div className="px-6 pb-6 pt-2">
          <div className="p-4 rounded-xl bg-blue-50/50 border border-blue-100">
            <h4 className="text-xs font-bold text-blue-600 uppercase tracking-widest mb-2 flex items-center gap-2">
              <svg
                xmlns="http://www.w3.org/2000/svg"
                className="h-4 w-4"
                fill="none"
                viewBox="0 0 24 24"
                stroke="currentColor"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
                />
              </svg>
              Explanation
            </h4>
            <div className="text-slate-700 text-md leading-relaxed whitespace-pre-wrap font-medium">
              {question.explanation || "No written explanation provided."}
            </div>
            {question.explanationImage && (
              <div className="mt-4 rounded-lg border border-blue-100 overflow-hidden bg-white w-fit">
                <img
                  src={question.explanationImage}
                  alt="Explanation"
                  className="max-h-[200px] w-auto object-contain p-1"
                />
              </div>
            )}
          </div>
        </div>
      )}
    </div>
  );
};

export default QuestionCard;

