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

begin;

select plan(17);

set search_path to camper, public;

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


set client_min_messages to warning;
truncate amenity_feature_i18n cascade;
truncate amenity_feature cascade;
truncate amenity_carousel_i18n cascade;
truncate amenity_carousel cascade;
truncate amenity_i18n cascade;
truncate amenity_feature_i18n 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, country_code, currency_code, default_lang_tag)
values (1, 'Company 1', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 'ES', 'EUR', 'ca')
     , (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, '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"};')
     , ('text/plain', 'hello, world!')
     , ('image/svg+xml', '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>')
;

insert into media (media_id, company_id, original_filename, content_hash)
values (3, 1, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
     , (4, 1, 'cover3.xpm', sha256('static char *s[]={"1 1 1 1","a c #ff00ff","a"};'))
     , (5, 2, 'cover4.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffff00","a"};'))
     , (6, 2, 'text.txt', sha256('hello, world!'))
     , (7, 2, 'image.svg', sha256('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>'))
;

insert into amenity (amenity_id, company_id, label, name)
values (12, 1, 'A1', 'Amenity A1')
     , (13, 2, 'A1', 'Amenity A1')
     , (14, 2, 'B1', 'Amenity B1')
;

insert into amenity_i18n (amenity_id, lang_tag)
values (12, 'ca')
     , (12, 'es')
     , (13, 'ca')
     , (13, 'es')
     , (14, 'ca')
     , (14, 'es')
;

insert into amenity_carousel (amenity_id, media_id, caption)
values (12, 3, 'Source caption')
     , (12, 4, 'Source caption')
     , (13, 5, 'Another caption')
     , (13, 6, 'N/A')
     , (14, 5, 'Another caption')
     , (14, 6, 'N/A')
;

insert into amenity_carousel_i18n (amenity_id, media_id, lang_tag, caption)
values (12, 3, 'en', 'Target caption')
     , (12, 3, 'es', 'Target caption (spanish)')
     , (12, 4, 'en', 'Target caption')
     , (12, 4, 'es', 'Target caption (spanish)')
     , (13, 5, 'en', 'Target caption')
     , (13, 5, 'es', 'Target caption (spanish)')
     , (13, 6, 'en', 'Target caption')
     , (13, 6, 'es', 'Target caption (spanish)')
     , (14, 5, 'en', 'Target caption')
     , (14, 5, 'es', 'Target caption (spanish)')
     , (14, 6, 'en', 'Target caption')
     , (14, 6, 'es', 'Target caption (spanish)')
;

insert into amenity_feature (amenity_feature_id, amenity_id, icon_name, name)
values (15, 12, 'baby', 'Baby')
     , (16, 12, 'information', 'Information')
     , (17, 13, 'castle', 'Castle')
     , (18, 13, 'barbecue', 'Barbecue')
     , (19, 14, 'bicycle', 'Bicycle')
     , (20, 14, 'puzzle', 'Puzzle')
;

insert into amenity_feature_i18n (amenity_feature_id, lang_tag, name)
values (15, 'ca', 'Nadó')
     , (15, 'es', 'Bebé')
     , (16, 'ca', 'Informació')
     , (16, 'es', 'Información')
     , (17, 'ca', 'Castell')
     , (17, 'es', 'Castillo')
     , (18, 'ca', 'Barbacoa')
     , (18, 'es', 'Barbacoa')
     , (19, 'ca', 'Bicicleta')
     , (19, 'es', 'Bicicleta')
     , (20, 'ca', 'Trencaclosques')
     , (20, 'es', 'Rompecabezas')
;

select lives_ok(
	$$ select remove_amenity(12) $$,
	'Should be able to delete an amenity from the first company'
);

select lives_ok(
	$$ select remove_amenity(14) $$,
	'Should be able to delete an amenity from the second company'
);

select bag_eq(
	$$ select amenity_feature_id, lang_tag, name from amenity_feature_i18n $$,
	$$ values (17, 'ca', 'Castell')
	        , (17, 'es', 'Castillo')
	        , (18, 'ca', 'Barbacoa')
	        , (18, 'es', 'Barbacoa')
	$$,
	'Should have removed features’ translations'
);

select bag_eq(
	$$ select amenity_id, icon_name, name from amenity_feature $$,
	$$ values (13, 'castle', 'Castle')
	        , (13, 'barbecue', 'Barbecue')
	$$,
	'Should have removed features'
);

select bag_eq(
	$$ select amenity_id, media_id, lang_tag, caption from amenity_carousel_i18n $$,
	$$ values (13, 5, 'en', 'Target caption')
	        , (13, 5, 'es', 'Target caption (spanish)')
	        , (13, 6, 'en', 'Target caption')
	        , (13, 6, 'es', 'Target caption (spanish)')
	$$,
	'Should have removed the slides’ translations'
);

select bag_eq(
	$$ select amenity_id, media_id, caption from amenity_carousel $$,
	$$ values (13, 5, 'Another caption')
	        , (13, 6, 'N/A')
	$$,
	'Should have removed the slides'
);

select bag_eq(
	$$ select amenity_id, lang_tag from amenity_i18n $$,
	$$ values (13, 'ca')
	        , (13, 'es')
	$$,
	'Should have removed the amenities'
);

select bag_eq(
	$$ select amenity_id, label from amenity $$,
	$$ values (13, 'A1') $$,
	'Should have removed the amenities'
);

select *
from finish();

rollback;