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

begin;

set search_path to camper, public;

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

create or replace function set_campsite_type_cost(slug uuid, season_id integer, min_nights integer, cost_per_night text, decimal_places integer default 2) returns void as
$$
	insert into campsite_type_cost (campsite_type_id, season_id, min_nights, cost_per_night)
	select campsite_type_id, season_id, min_nights, parse_price(cost_per_night, decimal_places)
	from campsite_type
	where campsite_type.slug = set_campsite_type_cost.slug
	on conflict (campsite_type_id, season_id) do update
	set min_nights = excluded.min_nights
	  , cost_per_night = excluded.cost_per_night
	;
$$
	language sql
;

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

commit;
