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

begin;

select plan(12);

set search_path to camper, public;

select has_function('camper', 'remove_surroundings_ad', array['integer']);
select function_lang_is('camper', 'remove_surroundings_ad', array['integer'], 'sql');
select function_returns('camper', 'remove_surroundings_ad', array['integer'], 'void');
select isnt_definer('camper', 'remove_surroundings_ad', array['integer']);
select volatility_is('camper', 'remove_surroundings_ad', array['integer'], 'volatile');
select function_privs_are('camper', 'remove_surroundings_ad', array['integer'], 'guest', array[]::text[]);
select function_privs_are('camper', 'remove_surroundings_ad', array['integer'], 'employee', array[]::text[]);
select function_privs_are('camper', 'remove_surroundings_ad', array['integer'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'remove_surroundings_ad', array['integer'], '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, 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 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', 'Anunci 2', 'Ves!')
     , (2, 'es', 'Anuncio 2', '¡Ve!')
     , (4, 'ca', 'Anunci 4', 'Ves!')
     , (4, 'es', 'Anuncio 4', '¡Ve!')
;

select lives_ok(
	$$ select remove_surroundings_ad(2) $$,
	'Should be able to delete the ad from the first company'
);

select bag_eq(
	$$ select company_id, media_id, title, anchor, href::text from surroundings_ad $$,
	$$ values (4, 8, 'Ad 4', 'Go!', 'https://ddg.gg/')
	$$,
	'The row should have been deleted.'
);

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


select *
from finish();

rollback;
