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

set client_min_messages to warning;
truncate season_i18n cascade;
truncate season 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, country_code, currency_code, default_lang_tag)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 'ES', 'EUR', 'ca')
;

insert into season (season_id, company_id, slug, name, color, active)
values (2, 1, '87452b88-b48f-48d3-bb6c-0296de64164e', 'High', to_integer('#232323'), true)
     , (3, 1, '9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Low', to_integer('#323232'), false)
;

insert into season_i18n (season_id, lang_tag, name)
values (3, 'ca', 'baixa')
;


select lives_ok(
	$$ select translate_season('87452b88-b48f-48d3-bb6c-0296de64164e', 'ca', 'Alta') $$,
	'Should be able to translate the first season'
);

select lives_ok(
	$$ select translate_season('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'es', 'Baja') $$,
	'Should be able to translate the second season'
);

select lives_ok(
	$$ select translate_season('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'ca', 'Baixa') $$,
	'Should be able to overwrite the catalan translation of the second season'
);

select bag_eq(
	$$ select slug::text, lang_tag, i18n.name from season_i18n as i18n join season using (season_id) $$,
	$$ values ('87452b88-b48f-48d3-bb6c-0296de64164e', 'ca', 'Alta')
	        , ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'ca', 'Baixa')
	        , ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'es', 'Baja')
	$$,
	'Should have added and updated all translations.'
);
select *
from finish();

rollback;