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


set client_min_messages to warning;
truncate payment_redsys_response cascade;
truncate payment_option cascade;
truncate payment cascade;
truncate campsite_type_option 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 campsite_type_option (campsite_type_option_id, campsite_type_id, name, range, per_night)
values (16, 12, 'Big tent', '[0, 4)', 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, zone_preferences, acsi_card, payment_status, created_at, updated_at)
values (22, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'draft', '2024-01-01 01:01:01', current_timestamp - interval '1 day, -1 second')
     , (24, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'draft', '2024-01-01 01:01:01', current_timestamp - interval '1 day, 1 second')
     , (26, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'pending', '2024-01-02 02:02:02', current_timestamp - interval '1 hour, -1 second')
     , (28, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'pending', '2024-01-03 03:03:03', current_timestamp - interval '1 hour, 1 second')
     , (30, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'preauth', '2024-01-04 04:04:04', current_timestamp - interval '2 days')
     , (32, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'failed', '2024-01-05 05:05:05', current_timestamp - interval '2 days')
     , (34, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'completed', '2024-01-05 05:05:05', current_timestamp - interval '2 days')
     , (36, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'refunded', '2024-01-05 05:05:05', current_timestamp - interval '2 days')
     , (38, 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'voided', '2024-01-05 05:05:05', current_timestamp - interval '2 days')
;

insert into payment_option (payment_id, campsite_type_option_id, units, subtotal)
select payment_id, 16, 1, 12 from payment;

insert into payment_redsys_response (payment_id, response_code, date_time, secure_payment, transaction_type, amount, currency_code, order_number, authorization_code, merchant_code, terminal_number, error_code)
select payment_id, 0, current_timestamp, true, 0, 123, 'EUR', '12#2', '123', '123', 1, '' from payment;

set role guest;

select lives_ok($$ select * from flush_payments() $$, 'Should be able to run the function as guest');

reset role;

select bag_eq(
	$$ select payment_id, payment_status, updated_at from payment $$,
	$$ values (22, 'draft', current_timestamp - interval '1 day, -1 second')
	        , (26, 'pending', current_timestamp - interval '1 hour, -1 second')
	        , (28, 'failed', current_timestamp)
	        , (30, 'preauth', current_timestamp - interval '2 days')
	        , (32, 'failed', current_timestamp - interval '2 days')
	        , (34, 'completed', current_timestamp - interval '2 days')
	        , (36, 'refunded', current_timestamp - interval '2 days')
	        , (38, 'voided', current_timestamp - interval '2 days')
	$$,
	'Should have removed expired drafts, and cancelled overdue pending orders'
);

select set_eq (
	$$ select payment_id from payment_option $$,
	$$ select payment_id from payment $$,
	'Should have deleted only draft options'
);

select set_eq (
	$$ select payment_id from payment_redsys_response $$,
	$$ select payment_id from payment $$,
	'Should have deleted only draft responses'
);

select *
from finish();

rollback;
