require 'csv'

# クイズのデータをCSVから読み込む
quizzes = { "初級" => [], "中級" => [], "上級" => [] }
CSV.foreach('quizzes.csv',headers: true) do |row|
  quizzes[row['level']] << {
    question: row['question'],
    choices: [row['choice1'],row['choice2'],row['choice3']],
    answer: row['answer']
  }
end

#index.htmlを生成
File.open('index.html','w') do |file|
  file.puts "<!DOCTYPE html>"
  file.puts "<html>"
  file.puts "<head>"
  file.puts "<title>クイズサイト</titlw>"
  file.puts "</head>"
  file.puts "<body>"
  file.puts "<hq>クイズサイト</h1>"
  file.puts "<form action='result.rb' method='post'>"
  ["初級","中級","上級"].each do |level|
    file.puts" <h2>#{level}</h2>"
    quizzes[level].each do |quiz|
    file.puts" <p>#{quiz[:question]}</p>"
    quiz[:choices].each do |choice|
      file.puts" <input type='radio'name='#{quiz[:question]}'value='#{choice}'>#{choice}<br>"
    end
   end
  end

  file.puts" <input type='submit'value='回答'>"
  file.puts"</form>"
  file.puts"</body>"
  file.puts"</htmle>"
end

puts "index.html has been generated"
