#!/usr/bin/env ruby

require 'sparql/client'
require 'rdf'
require 'pry'

found_one = File.open('found_countries_one.ttl', 'w')
found_zero = File.open('found_countries_zero.ttl', 'w')
found_many = File.open('found_countries_many.ttl', 'w')

wikidata = SPARQL::Client.new('https://query.wikidata.org/sparql')

# Vocabularies
wd = RDF::Vocabulary.new('http://www.wikidata.org/entity/')
wdt = RDF::Vocabulary.new('http://www.wikidata.org/prop/direct/')
rdfs = RDF::Vocabulary.new('http://www.w3.org/2000/01/rdf-schema#')

# binding.pry

# query = wikidata.select.where([:s, :p, :o]).limit(10)

# query.each_solution do |solution|
#   p solution
# end

require_relative 'database'

COUNTRIES = {}

DB[:location].each do |location|
  unless location[:Country].nil? or COUNTRIES.include? location[:Country]
    COUNTRIES[location[:Country]] = {
      id: "region:#{location[:country]}"
    }
  end
end

def country_definition(country_name_literal, wd_solutions)
  wd_ids = wd_solutions.map do |solution|
    country_uri = solution[:country].to_s
    get_wd_name(country_uri)
  end
  "country:#{toName(country_name_literal.to_s)} a tm:Country ;\n" \
  "  rdfs:label #{country_name_literal.to_ntriples} ;\n" \
  "  tm:wikidataID #{wd_ids.join(' , ')} .\n"
end

COUNTRIES.each do |country_name, country_attrs|
  p country_name
  country_name_literal = RDF::Literal.new(country_name, language: :en)
  query = wikidata.select
            .where([:country, rdfs.label, country_name_literal])
            .where([:country, wdt.P31, wd.Q6256])
  solutions = query.solutions
  case solutions.size
  when 0
    found_zero << "#{country_name}\n"
  when 1
    found_one << country_definition(country_name_literal, solutions)
  else
    found_many << country_definition(country_name_literal, solutions)
  end
  sleep(1)
end

