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

begin;

select plan(10);

set search_path to camper, public;

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


set client_min_messages to warning;
truncate payment_customer cascade;
truncate payment 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, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 350, 7, '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"};')
;

insert into media (media_id, company_id, original_filename, content_hash)
values (10, 2, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
;

insert into campsite_type (campsite_type_id, slug, company_id, name, media_id, max_campers, bookable_nights, overflow_allowed)
values (12, 'c1b6f4fc-32c1-4cd5-b796-0c5059152a52', 2, 'Plots', 10, 6, '[1, 7]', true)
;

insert into payment (payment_id, company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, currency_code, down_payment_percent, zone_preferences, acsi_card)
values (22, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', 1.0, '', false)
     , (23, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'EUR', 0.5, '', false)
     , (24, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2500, 'EUR', 0.75, '', false)
     , (25, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1234, 'EUR', 0.99, '', false)
     , (26, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 'EUR', 0.5, '', false)
     , (27, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 987654, 'EUR', 0.33, '', false)
     , (28, 2, 12, '2024-08-28', '2024-09-03', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 987654, 'EUR', 0.19, '', false)
;

select bag_eq(
	$$ select payment_id, payment.down_payment from payment $$,
	$$ values (22,      0)
	        , (23,      0)
	        , (24,   1875)
	        , (25,   1222)
	        , (26,      1)
	        , (27, 325926)
	        , (28, 187654)
	$$,
	'Should give out the down payment for each payment'
);


select *
from finish();

rollback;
