"""
In 2025, we have experimentally transported all of VOTT to DALIA

This is done by this script that just regtap-s all doc:Document records
and exports them as CSV.  Ouch.
"""

import csv
import pyvo


DIF_FIELDS = ("Authors,License,Link,Title,Community,Description,"
	"Discipline,FileFormat,Keywords,Language,LearningResourceType,"
	"MediaType,ProficiencyLevel,PublicationDate,TargetGroup,RelatedWork,"
	"Size,Version").split(",")


CONTENT_TYPE_MAP = {
	"Animation": "video",
	"Archive": "multipart",
	"Artwork": "image",
	"Background": "text",
	"BasicData": "multipart",
	"Bibliography": "multipart",
	"Catalog": "multipart",
	"EPOResource": "text",
	"Education": "text",
	"Historical": "text",
	"Journal": "text",
	"Library": "text",
	"Organisation": "multipart",
	"Other": "",
	"Outreach": "text",
	"Photographic": "image",
	"Press": "text",
	"Project": "multipart",
	"Registry": "multipart",
	"Simulation": "multipart",
	"Survey": "multipart",
	"Transformation": "multipart",
}

CONTENT_LEVEL_MAP = {
	"amateur": "teacher (school)",
	"general": "student (school)",
	"research": "researcher",
}


def map_hash_list(value, map):
	return " * ".join(map.get(v, v) for v in value.split("#"))


def make_DIF(f):
	writer = csv.DictWriter(f, DIF_FIELDS)
	writer.writeheader()
	for rec in pyvo.registry.regtap.get_RegTAP_service().run_sync("""
			SELECT
			    creator_seq as "Authors",
			    COALESCE(rights_uri, rights) as "License",
			    "Link",
			    res_title as "Title",
			    'IVOA' as "Community",
			    res_description as "Description",
			    'https://w3id.org/kim/hochschulfaechersystematik/n014' as "Discipline",
			    '' as "FileFormat",
			    "Keywords",
			    "Language",
			    'Tutorial' as "LearningResourceType",
			    'text' as "MediaType",
			    '' as "ProficiencyLevel",
			    COALESCE("PublicationDate", created) AS "PublicationDate",
			    content_level as "TargetGroup",
			    '' as "RelatedWork",
			    '' as "Size",
			    res_version as "Version"
			  FROM rr.resource
			  NATURAL LEFT OUTER JOIN (
			  	SELECT ivoid, ivo_string_agg(res_subject, ' * ')
			    AS "Keywords" FROM rr.res_subject GROUP BY ivoid) AS kws
			  NATURAL LEFT OUTER JOIN (
			  	SELECT ivoid, ivo_string_agg(detail_value, ' * ')
			     AS "Language" FROM rr.res_detail
			   WHERE detail_xpath='/capability/languageCode'
			   GROUP BY ivoid) AS langs
			  NATURAL LEFT OUTER JOIN (SELECT ivoid, ivo_string_agg(access_url, ' * ')
			     AS "Link" FROM rr.interface
			   WHERE intf_role='rendered'
			   GROUP BY ivoid) AS links
			  NATURAL LEFT JOIN (
			  	SELECT ivoid, MAX(date_value) AS "PublicationDate"
			  	FROM rr.res_date
			  	WHERE value_role='inspected'
			  	GROUP BY ivoid) AS dates
			  WHERE res_type='doc:document'"""):
		row = dict(rec.items())

		row["Authors"] = row["Authors"].replace(';', " *")
		row["MediaType"] = map_hash_list(row["MediaType"], CONTENT_TYPE_MAP)
		row["TargetGroup"] = map_hash_list(row["TargetGroup"], CONTENT_LEVEL_MAP)

		writer.writerow(row)


if __name__=="__main__":
	with open("vo-docs-for-dalia.csv", "w", encoding="utf-8") as f:
		make_DIF(f)
