-- Test edit_payment_account_card
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_card', array['uuid', 'text', 'text', 'date']);
select function_lang_is('numerus', 'edit_payment_account_card', array['uuid', 'text', 'text', 'date'], 'plpgsql');
select function_returns('numerus', 'edit_payment_account_card', array['uuid', 'text', 'text', 'date'], 'uuid');
select isnt_definer('numerus', 'edit_payment_account_card', array['uuid', 'text', 'text', 'date']);
select volatility_is('numerus', 'edit_payment_account_card', array['uuid', 'text', 'text', 'date'], 'volatile');

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


set client_min_messages to warning;
truncate payment_account_card 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', 'card', 'Card 1')
     , (12, 1, '84e84788-54f1-4796-b93d-dfa2c9adc072', 'card', 'Card 2')
     , (13, 1, 'afb774a5-0c38-480c-bef2-fe03465071cd', 'other', 'Other')
;

insert into payment_account_card (payment_account_id, last_four_digits, expiration_date)
values (11, '1234', '2022-04-02')
     , (12, '2345', '2024-05-02')
;


select lives_ok(
	$$ select edit_payment_account_card('81c478ab-395c-44aa-a5b4-3489e5349a0b', 'Gold Card', '9999', '2022-12-22') $$,
	'Should be able to edit the first card payment account'
);

select lives_ok (
	$$ select edit_payment_account_card('84e84788-54f1-4796-b93d-dfa2c9adc072', 'Scurvy Card', '7777', '2025-08-25') $$,
	'Should be able to edit the second card payment account'
);

select results_eq (
	$$ select edit_payment_account_card('afb774a5-0c38-480c-bef2-fe03465071cd', 'Huh?', '1234', '2006-01-02')::text $$,
	$$ values (null::text) $$,
	'Should do nothing to an account that is not of type card'
);

select bag_eq(
	$$ select payment_account_id, payment_account_type::text, name, last_four_digits, expiration_date::text from payment_account left join payment_account_card using (payment_account_id, payment_account_type) $$,
	$$ values (11, 'card', 'Gold Card', '9999', '2022-12-22')
	        , (12, 'card', 'Scurvy Card', '7777', '2025-08-25')
	        , (13, 'other', 'Other', null::text, null::text)
	$$,
	'Should have update all card payment accounts'
);


select *
from finish();

rollback;
