#!/usr/bin/env ruby

require_relative 'database'
require_relative 'vocabularies'

# Output files of this step
found_one = File.open(File.join('data', '001-found_countries_one.ttl'), 'w')
found_zero = File.open(File.join('data', '001-found_countries_zero.ttl'), 'w')
found_many = File.open(File.join('data', '001-found_countries_many.ttl'), 'w')
files = [found_one, found_zero, found_many]

files.each do |file|
  file.puts prefixes(:wd, :wdt, :rdfs, :mig, :schema, :country)
  file.puts
end

countries = {}

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

def country_definition(country_name_literal, wd_solutions)
  wd_ids = []
  geo_ids = []
  wd_solutions.map do |solution|
    country_uri = solution[:country].to_s
    wd_ids << get_wd_name(country_uri)
    geo_ids << "geo:#{solution[:GeoNamesID]}"
  end
  "country:#{toName(country_name_literal.to_s)} a schema:Country ;\n" \
  "  rdfs:label #{country_name_literal.to_ntriples} ;\n" \
  "  tm:wikidataEntity #{wd_ids.join(' , ')} ;\n" \
  "  tm:geonamesEntiy #{geo_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])
            .where([:country, WDT.P1566, :GeoNamesID])
  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

