function IconDoc() {
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" style={{ display: "inline", verticalAlign: "-2px", marginRight: 6 }}>
      <rect x="2" y="1" width="10" height="13" rx="1.5" stroke="currentColor" strokeWidth="1.4" fill="none" />
      <line x1="4.5" y1="5"  x2="9.5" y2="5"  stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" />
      <line x1="4.5" y1="7.5" x2="9.5" y2="7.5" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" />
      <line x1="4.5" y1="10" x2="7.5" y2="10" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" />
    </svg>
  );
}

function IconUsers() {
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" style={{ display: "inline", verticalAlign: "-2px", marginRight: 6 }}>
      <circle cx="6" cy="5" r="2.2" stroke="currentColor" strokeWidth="1.3" fill="none" />
      <path d="M1.5 13c0-2.5 2-4 4.5-4s4.5 1.5 4.5 4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" fill="none" />
      <circle cx="11.5" cy="5" r="1.8" stroke="currentColor" strokeWidth="1.2" fill="none" />
      <path d="M13 13c0-1.8-0.8-3-2-3.6" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" fill="none" />
    </svg>
  );
}

function IconCheck() {
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" style={{ display: "inline", verticalAlign: "-2px", marginRight: 6 }}>
      <circle cx="8" cy="8" r="6.3" stroke="currentColor" strokeWidth="1.4" fill="none" />
      <path d="M5 8.5l2 2 4-4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" fill="none" />
    </svg>
  );
}

interface DataViewerProps {
  parsedData: Record<string, unknown[]>;
}

export function DataViewer({ parsedData }: DataViewerProps) {
  const questions = parsedData.questions?.[0] as { structured_data: { questions: Array<{ number: number; text: string; type: string; options: string[] | null; points: number | null }> } } | undefined;
  const studentAnswers = parsedData.student_answers?.[0] as { structured_data: { students: Array<{ name: string; id: string; answers: Array<{ question_number: number; answer: string }> }> } } | undefined;
  const teacherAnswers = parsedData.teacher_answers?.[0] as { structured_data: { answers: Array<{ question_number: number; correct_answer: string; explanation: string | null }> } } | undefined;

  return (
    <div className="space-y-6">
      {/* Questions */}
      {questions && (
        <div className="card p-6">
          <h3 className="font-display text-lg font-semibold text-[var(--color-text)] mb-6">
            <IconDoc />考試題目 ({questions.structured_data.questions?.length || 0} 題)
          </h3>
          <div className="overflow-x-auto">
            <table className="w-full text-sm">
              <thead>
                <tr className="border-b border-[var(--color-border)]">
                  <th className="text-left py-2 px-3 text-[var(--color-text-muted)]">#</th>
                  <th className="text-left py-2 px-3 text-[var(--color-text-muted)]">Theme</th>
                  <th className="text-left py-2 px-3 text-[var(--color-text-muted)]">Type</th>
                  <th className="text-left py-2 px-3 text-[var(--color-text-muted)]">Score</th>
                </tr>
              </thead>
              <tbody>
                {questions.structured_data.questions?.map((q) => (
                  <tr key={q.number} className="border-b border-[var(--color-border)]/50">
                    <td className="py-2 px-3 text-[var(--color-primary)] font-bold">{q.number}</td>
                    <td className="py-2 px-3 text-[var(--color-text)]">
                      <p>{q.text}</p>
                      {q.options && (
                        <div className="mt-1 text-xs text-[var(--color-text-muted)]">
                          {q.options.join(" | ")}
                        </div>
                      )}
                    </td>
                    <td className="py-2 px-3">
                      <span className="badge badge-success">{q.type}</span>
                    </td>
                    <td className="py-2 px-3 text-[var(--color-text-muted)]">{q.points ?? "-"}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      )}

      {/* Student Answers */}
      {studentAnswers && (
        <div className="card p-6">
          <h3 className="font-display text-lg font-semibold text-[var(--color-text)] mb-6">
            <IconUsers />學生答案 ({studentAnswers.structured_data.students?.length || 0} 位學生)
          </h3>
          <div className="overflow-x-auto">
            <table className="w-full text-sm">
              <thead>
                <tr className="border-b border-[var(--color-border)]">
                  <th className="text-left py-2 px-3 text-[var(--color-text-muted)]">學生</th>
                  {questions?.structured_data.questions?.map((q) => (
                    <th key={q.number} className="text-center py-2 px-3 text-[var(--color-text-muted)]">Q{q.number}</th>
                  ))}
                </tr>
              </thead>
              <tbody>
                {studentAnswers.structured_data.students?.map((s) => (
                  <tr key={s.name || s.id} className="border-b border-[var(--color-border)]/50">
                    <td className="py-2 px-3 text-[var(--color-text)] font-medium">{s.name || s.id}</td>
                    {(questions?.structured_data.questions || s.answers)?.map((_, i) => {
                      const ans = s.answers?.find((a) => a.question_number === i + 1);
                      return (
                        <td key={i} className="text-center py-2 px-3 text-[var(--color-text-muted)]">
                          {ans?.answer ?? "-"}
                        </td>
                      );
                    })}
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      )}

      {/* Teacher Answers */}
      {teacherAnswers && (
        <div className="card p-6">
          <h3 className="font-display text-lg font-semibold text-[var(--color-text)] mb-6">
            <IconCheck />標準答案
          </h3>
          <div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-3">
            {teacherAnswers.structured_data.answers?.map((a) => (
              <div key={a.question_number} className="bg-[var(--color-background)] rounded-lg p-3">
                <span className="text-xs text-[var(--color-text-muted)]">Q{a.question_number}</span>
                <p className="text-[var(--color-success)] font-semibold">{a.correct_answer}</p>
              </div>
            ))}
          </div>
        </div>
      )}

      {!questions && !studentAnswers && !teacherAnswers && (
        <div className="text-center py-12 text-[var(--color-text-muted)]">
          No analysis data available yet.
        </div>
      )}
    </div>
  );
}
