-- Test translate_legal_text
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(13);

set search_path to camper, public;

select has_function('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text']);
select function_lang_is('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'sql');
select function_returns('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'void');
select isnt_definer('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text']);
select volatility_is('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'volatile');
select function_privs_are('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'guest', array[]::text[]);
select function_privs_are('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'employee', array[]::text[]);
select function_privs_are('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'translate_legal_text', array['integer', 'text', 'text', 'text', 'text'], 'authenticator', array[]::text[]);

set client_min_messages to warning;
truncate legal_text_i18n cascade;
truncate legal_text cascade;
truncate company cascade;
reset client_min_messages;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, rtc_number, tourist_tax, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
     , (4, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', '', 60, 7, 'FR', 'USD', 'ca')
;

insert into legal_text (company_id, slug, name, content)
values (2, 'tos', 'Terms of Service', '<p>Go away</p>')
     , (4, 'cookies', 'Cookies', '<p>Yummy</p>')
;

insert into legal_text_i18n (company_id, slug, lang_tag, name, content)
values (2, 'tos', 'ca', '<p>Termes i condicions</p>', '<p>Uh?</p>')
     , (4, 'cookies', 'ca', '<p>Galetes</p>', '<p>Uh?</p>')
;

select lives_ok(
	$$ select translate_legal_text(2, 'tos', 'es', 'Términos', '<p>Adiós</p>') $$,
	'Should be able to translate the legal text of the first company to a new language'
);

select lives_ok(
	$$ select translate_legal_text(2, 'tos', 'ca', 'Termes', '<p>Adéu</p>') $$,
	'Should be able to overwrite a legal text’s translation'
);

select lives_ok(
	$$ select translate_legal_text(4, 'cookies', 'ca', null, null) $$,
	'Should be able to “translate” a legal_text to empty strings'
);


select bag_eq(
	$$ select company_id, slug, lang_tag, name, content::text from legal_text_i18n $$,
	$$ values (2, 'tos', 'ca', 'Termes', '<p>Adéu</p>')
	        , (2, 'tos', 'es', 'Términos', '<p>Adiós</p>')
	        , (4, 'cookies', 'ca', '', '')
	$$,
	'Should have translated all legal texts'
);


select *
from finish();

rollback;
