require 'csv'
require 'date'
def load_fortunes(filename)
  fortunes = {}
  CSV.foreach(filename) do |row|
    character,message = row[0],row[1]
    fortunes[character] = message
  end
  fortunes
end

def calculate_fortune_index(birthdate)
  today = Date.today
  birth_sum = birthdate.month+birthdate.day
  today_sum = today.month+ today.day
  index = birth_sum % today_sum
  index
end
fortunes = load_fortunes('kimetsu_fortunes.csv')
  characters = ["たんじろう","ねずこ","ぜんいつ","いのすけ","れんごく","ぎゆう"]
  
puts "あなたの名前を入力してください:"
name = gets.chomp

while name .empty?
  puts "名前を入力してください:"
  name = gets chomp
end

puts "あなたの生年月日を入力してください(例:2015-01-05):"
birthdate_input=gets.chomp

while birthdate_input.empty? || !(birthdate_input =~ /^\d{4}-\d{2}-\d{2}$/)
  puts "正しい生年月日を入力してください(例:2015-01-05):"
  birthdate_input = gets.chomp
end
  
birthdate = Date.strptime(birthdate_input, '%Y-%m-%d')
index = calculate_fortune_index(birthdate)
character = characters[index % characters.length]
fortune = fortunes[character]

puts "#{name}さんの生年月日から導かれたキャラクターは#{character}です!今日の運勢は..."
puts fortune


