-- Deploy camper:set_campsite_type_cost to pg
-- requires: roles
-- requires: schema_camper
-- requires: campsite_type_cost
-- requires: parse_price
-- requires: campsite_type__bookable_nights
-- requires: campsite_type_cost__per_age

begin;

set search_path to camper, public;

drop function if exists set_campsite_type_cost(uuid, integer, integer, text, integer);

create or replace function set_campsite_type_cost(slug uuid, season_id integer, per_night text, per_adult text, per_teenager text, per_child text) returns void as
$$
	insert into campsite_type_cost (campsite_type_id, season_id, cost_per_night, cost_per_adult, cost_per_teenager, cost_per_child)
	select campsite_type_id, season_id, parse_price(per_night, decimal_digits), parse_price(per_adult, decimal_digits), parse_price(per_teenager, decimal_digits), parse_price(per_child, decimal_digits)
	from campsite_type
		join company using (company_id)
		join currency using (currency_code)
	where campsite_type.slug = set_campsite_type_cost.slug
	on conflict (campsite_type_id, season_id) do update
	set cost_per_night = excluded.cost_per_night
	  , cost_per_adult = excluded.cost_per_adult
	  , cost_per_teenager = excluded.cost_per_teenager
	  , cost_per_child = excluded.cost_per_child
	;
$$
	language sql
;

revoke execute on function set_campsite_type_cost(uuid, integer, text, text, text, text) from public;
grant execute on function set_campsite_type_cost(uuid, integer, text, text, text, text) to admin;

commit;
