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

begin;

select plan(15);

set search_path to auth, numerus, public;

select has_function('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]']);
select function_lang_is('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'plpgsql');
select function_returns('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'boolean');
select isnt_definer('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]']);
select volatility_is('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'volatile');
select function_privs_are('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_product', array ['uuid', 'text', 'text', 'text', 'integer[]'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate product_tax cascade;
truncate product cascade;
truncate tax 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, country_code, currency_code)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR')
     , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD')
;

insert into tax (tax_id, company_id, name, rate)
values (3, 1, 'IRPF -15 %', -0.15)
     , (4, 1, 'IVA 21 %', 0.21)
     , (5, 2, 'IRPF -7 %', -0.07)
     , (6, 2, 'IVA 10 %', 0.10)
;

insert into product (product_id, company_id, slug, name, description, price)
values (7, 1, 'd2300404-bd23-48b3-8e2b-2bbf86dc7bd6', 'Product 01', 'Description01', 1200)
     , (8, 2, '2f085b8b-da90-41fe-b8cf-6ba8d94cfa38', 'Product 02', 'Description02', 2400)
     , (9, 2, '84044d0b-af33-442a-95a6-21efc77260d5', 'Product 03', 'Description03', 3600)
;

insert into product_tax (product_id, tax_id)
values (7, 3)
     , (8, 5)
     , (9, 5)
     , (9, 6)
;

select is(
	edit_product('d2300404-bd23-48b3-8e2b-2bbf86dc7bd6', 'Product 1', 'Description 1', '12.12', array[3, 4]),
	true,
	'Should be able to edit product from first company'
);

select is(
	edit_product('2f085b8b-da90-41fe-b8cf-6ba8d94cfa38', 'Product 2', 'Description 2', '24.24', array[6]),
	true,
	'Should be able to edit product from second company'
);

select is(
	edit_product('84044d0b-af33-442a-95a6-21efc77260d5', 'Product 3', 'Description 3', '36.36', array[]::integer[]),
	true,
	'Should be able to edit a product a remove all taxes'
);

select is(
	edit_product('87e158d1-a0f5-48a7-854b-b86d7b4bb21c', 'Product 4', 'Description 4', '48.48', array[]::integer[]),
	false,
	'Should return false when the product does not exist'
);

select bag_eq(
	$$ select product_id, company_id, name, description, price from product $$,
	$$ values (7, 1, 'Product 1', 'Description 1', 1212)
	        , (8, 2, 'Product 2', 'Description 2', 2424)
		, (9, 2, 'Product 3', 'Description 3', 3636)
	$$,
	'Should have edited all three products'
);

select bag_eq(
	$$ select product_id, tax_id from product_tax $$,
	$$ values (7, 3)
	        , (7, 4)
		, (8, 6)
	$$,
	'Should have updated the taxes for the products we told to'
);

select *
from finish();

rollback;
