#!/usr/bin/env ruby

require_relative 'database'
require_relative 'vocabularies'
require_relative 'migrants'

output = File.open(File.join('data', 'persons_religions.ttl'), 'w')

output.puts prefixes(:rdfs, :tm, :schema, :person, :religion, :xsd)
output.puts

RELIGION_MAP = {
  'Buddhism'  => 'religion:Buddhism',
  'Christian' => 'religion:Christianity',
  'Hinduism'  => 'religion:Hinduism',
  'Jewish'    => 'religion:Judaism',
  'Muslim'    => 'religion:Islam'
}

DB[:religions].each do |row|
  id = row[:IDReligion]
  props = ["a tm:ReligionAffiliation"]

  props << "tm:person person:#{row[:IDPerson]}" unless row[:IDPerson].to_s.empty?
  religion_uri = RELIGION_MAP[row[:religion]]
  props << "tm:religion #{religion_uri}" if religion_uri
  props << "tm:denomination #{ttl_literal(row[:denomination])}" unless row[:denomination].to_s.empty?
  props << "tm:dateStartFuzzy #{ttl_literal(row[:DateStart_Fuzzy])}" unless row[:DateStart_Fuzzy].to_s.empty?

  props << "tm:dateStart #{ttl_literal(row[:date_start])}" if row[:date_start]
  props << "tm:dateStartMin #{ttl_literal(row[:DateStart_Min])}" if row[:DateStart_Min]
  props << "tm:dateStartMax #{ttl_literal(row[:DateStart_Max])}" unless row[:DateStart_Max].to_s.empty?
  props << "tm:dateEndMin #{ttl_literal(row[:DateEnd_Min])}" if row[:DateEnd_Min]
  props << "tm:dateEndMax #{ttl_literal(row[:DateEnd_Max])}" if row[:DateEnd_Max]

  props << "rdfs:comment #{ttl_literal(row[:comment])}" unless row[:comment].to_s.empty?

  output.puts "religion:#{id} #{props.first} ;"
  props[1..-2].each { |p| output.puts "  #{p} ;" }
  output.puts "  #{props.last} ."
  output.puts
end

output.close
