-- Test translate_campsite_type
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_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text']);
select function_lang_is('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'sql');
select function_returns('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'void');
select isnt_definer('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text']);
select volatility_is('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'volatile');
select function_privs_are('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'guest', array[]::text[]);
select function_privs_are('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'employee', array[]::text[]);
select function_privs_are('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'translate_campsite_type', array['uuid', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text'], 'authenticator', array[]::text[]);


set client_min_messages to warning;
truncate campsite_type cascade;
truncate media cascade;
truncate media_content 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 (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
;

insert into media_content (media_type, bytes)
values ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffffff","a"};')
     , ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ff00ff","a"};')
     , ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffff00","a"};')
;

insert into media (media_id, company_id, original_filename, content_hash)
values (2, 1, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
     , (3, 1, 'cover3.xpm', sha256('static char *s[]={"1 1 1 1","a c #ff00ff","a"};'))
     , (4, 1, 'cover4.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffff00","a"};'))
;

insert into campsite_type (company_id, slug, media_id, name, spiel, info, facilities, description, additional_info, check_in, check_out, active, max_campers, bookable_nights)
values (1, '87452b88-b48f-48d3-bb6c-0296de64164e', 2, 'Type A', '<h2>One</h2>', '<p>Features A</p>', '<p>Pricing A</p>', '<p>A</p>', '<p>Additional A</p>', '', '', true, 4, '[1, 7]')
     , (1, '9b6370f7-f941-46f2-bc6e-de455675bd0a', 3, 'Type B', '<h2>Two</h2>', '<p>Features B</p>', '<p>Pricing B</p>', '<p>B</p>', '<p>Additional B</p>', '', '', false, 5, '[2, 6]')
;

insert into campsite_type_i18n (campsite_type_id, lang_tag, name, spiel, info, facilities, description, additional_info, check_in, check_out)
select campsite_type_id, 'ca', 'tipusb', '<h2>dos</h2>', '<p>característiques b</p>', '<p>preus b</p>', '<p>B</p>', '<p>B addicional</p>', 'entrada', 'sortida' from campsite_type
where slug = '9b6370f7-f941-46f2-bc6e-de455675bd0a'
;

select lives_ok(
	$$ select translate_campsite_type('87452b88-b48f-48d3-bb6c-0296de64164e', 'ca', 'Tipus A', null, '<p>Característiques A</p>', null, '<p>a</p>', null, 'CI1', null) $$,
	'Should be able to translate the first type'
);

select lives_ok(
	$$ select translate_campsite_type('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'es', 'Tipo B', '<h2>Dos</h2>', null, '<p>Precios B</p>', null, '<p>B adicional</p>', null, 'CO2') $$,
	'Should be able to translate the second type'
);

select lives_ok(
	$$ select translate_campsite_type('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'ca', 'Tipus B', '<h2>Dos</h2>', '<p>Característiques B</p>', '<p>Preus B</p>', '<p>2</p>', '<p>2 addicional</p>', 'E2', 'S2') $$,
	'Should be able to overwrite the catalan translation of the second type'
);

select bag_eq(
	$$ select slug::text, lang_tag, i18n.name, i18n.spiel::text, i18n.info::text, i18n.facilities::text, i18n.description::text, i18n.additional_info::text, i18n.check_in, i18n.check_out from campsite_type_i18n as i18n join campsite_type using (campsite_type_id) $$,
	$$ values ('87452b88-b48f-48d3-bb6c-0296de64164e', 'ca', 'Tipus A', '', '<p>Característiques A</p>', '', '<p>a</p>', '', 'CI1', '')
	        , ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'ca', 'Tipus B', '<h2>Dos</h2>', '<p>Característiques B</p>', '<p>Preus B</p>', '<p>2</p>', '<p>2 addicional</p>', 'E2', 'S2')
	        , ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'es', 'Tipo B', '<h2>Dos</h2>', '', '<p>Precios B</p>', '', '<p>B adicional</p>', '', 'CO2')
	$$,
	'Should have added and updated all translations.'
);


select *
from finish();

rollback;
