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


set client_min_messages to warning;
truncate surroundings_ad_i18n cascade;
truncate surroundings_ad cascade;
truncate media cascade;
truncate media_content cascade;
truncate company_host cascade;
truncate company_user cascade;
truncate company cascade;
truncate auth."user" cascade;
reset client_min_messages;

insert into auth."user" (user_id, email, name, password, cookie, cookie_expires_at)
values (1, 'demo@tandem.blog', 'Demo', 'test', '44facbb30d8a419dfd4bfbc44a4b5539d4970148dfc84bed0e', current_timestamp + interval '1 month')
     , (5, 'admin@tandem.blog', 'Demo', 'test', '12af4c88b528c2ad4222e3740496ecbc58e76e26f087657524', current_timestamp + interval '1 month')
;

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

insert into company_user (company_id, user_id, role)
values (2, 1, 'admin')
     , (4, 5, 'admin')
;

insert into company_host (company_id, host)
values (2, 'co2')
     , (4, 'co4')
;

insert into media_content (media_type, bytes)
values ('text/plain', 'content2')
     , ('text/plain', 'content4')
;

insert into media (media_id, company_id, original_filename, content_hash)
values ( 7, 2, 'text2.txt', sha256('content2'))
     , ( 8, 4, 'text3.txt', sha256('content4'))
;

insert into surroundings_ad (company_id, media_id, title, anchor, href)
values (2, 7, 'Ad 2', 'Go!', 'https://ddg.gg/')
     , (4, 8, 'Ad 4', 'Go!', 'https://ddg.gg/')
;

insert into surroundings_ad_i18n (company_id, lang_tag, title, anchor)
values (2, 'ca', 'anun 2', 'au!')
;

select lives_ok(
	$$ select translate_surroundings_ad(4, 'es', 'Anuncio 4', '¡Ve!') $$,
	'Should be able to translate the ad from the first company to Spanish'
);

select lives_ok(
	$$ select translate_surroundings_ad(4, 'ca', 'Anunci 4', 'Ves!') $$,
	'Should be able to translate the ad from the first company to Catalan'
);

select lives_ok(
	$$ select translate_surroundings_ad(2, 'ca', 'Anunci 2', 'Ups!') $$,
	'Should be able to update Catalan translation of the ad from the first company'
);

select bag_eq(
	$$ select company_id, lang_tag, title, anchor from surroundings_ad_i18n $$,
	$$ values (2, 'ca', 'Anunci 2', 'Ups!')
	        , (4, 'ca', 'Anunci 4', 'Ves!')
	        , (4, 'es', 'Anuncio 4', '¡Ve!')
	$$,
	'The translations should have been updated.'
);


select *
from finish();

rollback;
