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

begin;

select plan(13);

set search_path to numerus, public;

select has_function('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban']);
select function_lang_is('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'plpgsql');
select function_returns('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'uuid');
select isnt_definer('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban']);
select volatility_is('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'volatile');

select function_privs_are('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'guest', array []::text[]);
select function_privs_are('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_payment_account_bank', array['uuid', 'text', 'iban'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate payment_account_bank cascade;
truncate payment_account cascade;
truncate payment_method cascade;
truncate company cascade;
reset client_min_messages;

set constraints "company_default_payment_method_id_fkey" deferred;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, default_payment_method_id)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 111)
;

insert into payment_method (payment_method_id, company_id, name, instructions)
values (111, 1, 'cash', 'cash')
;

set constraints "company_default_payment_method_id_fkey" immediate;

insert into payment_account (payment_account_id, company_id, slug, payment_account_type, name)
values (11, 1, '81c478ab-395c-44aa-a5b4-3489e5349a0b', 'bank', 'Bank 1')
     , (12, 1, '84e84788-54f1-4796-b93d-dfa2c9adc072', 'bank', 'Bank 2')
     , (13, 1, 'afb774a5-0c38-480c-bef2-fe03465071cd', 'other', 'Other')
;

insert into payment_account_bank (payment_account_id, iban)
values (11, 'MR1765838885255712383711884')
     , (12, 'UA709147419869457296562612646')
;


select lives_ok(
	$$ select edit_payment_account_bank('81c478ab-395c-44aa-a5b4-3489e5349a0b', 'Piggy Bank', 'ES2820958297603648596978') $$,
	'Should be able to edit the first bank payment account'
);

select lives_ok (
	$$ select edit_payment_account_bank('84e84788-54f1-4796-b93d-dfa2c9adc072', 'Totally Secure', 'DE68500105178297336485') $$,
	'Should be able to edit the second bank payment account'
);

select results_eq (
	$$ select edit_payment_account_bank('afb774a5-0c38-480c-bef2-fe03465071cd', 'Huh?', 'ES3720951423184941926287')::text $$,
	$$ values (null::text) $$,
	'Should do nothing to an account that is not of type bank'
);

select bag_eq(
	$$ select payment_account_id, payment_account_type::text, name, iban::text from payment_account left join payment_account_bank using (payment_account_id, payment_account_type) $$,
	$$ values (11, 'bank', 'Piggy Bank', 'ES2820958297603648596978')
	        , (12, 'bank', 'Totally Secure', 'DE68500105178297336485')
	        , (13, 'other', 'Other', null)
	$$,
	'Should have update all bank payment accounts'
);


select *
from finish();

rollback;
