-- Deploy camper:translate_campsite_type to pg
-- requires: roles
-- requires: schema_camper
-- requires: campsite_type_i18n

begin;

set search_path to camper, public;

drop function if exists translate_campsite_type (uuid, text, text, text, text, text, text);

create or replace function translate_campsite_type (slug uuid, lang_tag text, name text, spiel text, info text, facilities text, description text, additional_info text) returns void as
$$
	insert into campsite_type_i18n (campsite_type_id, lang_tag, name, spiel, info, facilities, description, additional_info)
	select campsite_type_id, translate_campsite_type.lang_tag, translate_campsite_type.name, xmlparse(content coalesce(translate_campsite_type.spiel, '')), xmlparse(content coalesce(translate_campsite_type.info, '')), xmlparse(content coalesce(translate_campsite_type.facilities, '')), xmlparse(content coalesce(translate_campsite_type.description, '')), xmlparse(content coalesce(translate_campsite_type.additional_info, ''))
	from campsite_type
	where slug = translate_campsite_type.slug
	on conflict (campsite_type_id, lang_tag) do update
	set name = excluded.name
	  , spiel = excluded.spiel
	  , info = excluded.info
	  , facilities = excluded.facilities
	  , description = excluded.description
	  , additional_info = excluded.additional_info
$$
	language sql
;

revoke execute on function translate_campsite_type (uuid, text, text, text, text, text, text, text) from public;
grant execute on function translate_campsite_type(uuid, text, text, text, text, text, text, text) to admin;

commit;
