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


set client_min_messages to warning;
truncate amenity_feature_i18n cascade;
truncate amenity_feature cascade;
truncate amenity 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 amenity (amenity_id, company_id, label, name)
values (11, 1, 'A1', 'Amenity A1')
     , (12, 1, 'A2', 'Amenity A2')
;

insert into amenity_feature (amenity_feature_id, amenity_id, icon_name, name)
values (13, 11, 'wifi', 'Feature 1')
     , (14, 11, 'baby', 'Feature 2')
     , (15, 12, 'castle', 'Feature 3')
     , (16, 12, 'ecofriendly', 'Feature 4')
;

insert into amenity_feature_i18n (amenity_feature_id, lang_tag, name)
values (13, 'ca', 'Característica 1')
     , (13, 'es', 'ES 1')
     , (14, 'ca', 'Característica 2')
     , (14, 'es', 'ES 2')
     , (15, 'ca', 'Característica 3')
     , (15, 'es', 'ES 3')
     , (16, 'ca', 'Característica 4')
     , (16, 'es', 'ES 4')
;

select lives_ok(
	$$ select remove_amenity_feature(13) $$,
	'Should be able to delete a feature from the first amenity'
);

select lives_ok(
	$$ select remove_amenity_feature(16) $$,
	'Should be able to delete a feature from the second amenity'
);

select bag_eq(
	$$ select amenity_feature_id, name from amenity_feature $$,
	$$ values (14, 'Feature 2')
	        , (15, 'Feature 3')
	$$,
	'Should have removed the features'
);

select bag_eq(
	$$ select amenity_feature_id, lang_tag, name from amenity_feature_i18n $$,
	$$ values (14, 'ca', 'Característica 2')
	        , (14, 'es', 'ES 2')
	        , (15, 'ca', 'Característica 3')
	        , (15, 'es', 'ES 3')
	$$,
	'Should have removed the features’ translations'
);


select *
from finish();

rollback;
