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

begin;

set search_path to camper, public;

select plan(14);

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


set client_min_messages to warning;
truncate campsite cascade;
truncate campsite_type 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 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 'ES', 'EUR', 'ca')
     , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', '', 60, 'FR', 'USD', 'ca')
;

insert into media_content (media_type, bytes)
values ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffffff","a"};')
;

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, 2, 'cover4.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
;

insert into campsite_type (campsite_type_id, company_id, media_id, name, dogs_allowed, max_campers, bookable_nights)
values (11, 1, 3, 'A', false, 5, '[1, 7]')
     , (12, 1, 3, 'B', false, 5, '[2, 6]')
     , (21, 2, 4, 'C', false, 5, '[3, 5]')
;

select lives_ok(
	$$ select add_campsite(11, 'A1', '<p>A1.1</p>', '<p>A1.2</p>') $$,
	'Should be able to add a campsite to the first company'
);

select lives_ok(
	$$ select add_campsite(12, 'B1', '<p>B1.1</p>', '<p>B1.2</p>') $$,
	'Should be able to add a campsite to the same company, but of a different type'
);

select lives_ok(
	$$ select add_campsite(21, 'C1', '<p>C1.1</p>', '<p>C1.2</p>') $$,
	'Should be able to add a campsite to the second company'
);

select throws_ok(
	$$ select add_campsite(22, 'C1', '', '') $$,
	'23503', 'insert or update on table "campsite" violates foreign key constraint "campsite_campsite_type_id_fkey"',
	'Should raise an error if the campsite type is not valid.'
);

select bag_eq(
	$$ select company_id, campsite_type_id, label, info1::text, info2::text, active from campsite $$,
	$$ values (1, 11, 'A1', '<p>A1.1</p>', '<p>A1.2</p>', true)
	        , (1, 12, 'B1', '<p>B1.1</p>', '<p>B1.2</p>', true)
	        , (2, 21, 'C1', '<p>C1.1</p>', '<p>C1.2</p>', true)
	$$,
	'Should have added all campsites'
);

select *
from finish();

rollback;