-- Deploy camper:add_amenity_carousel_slide to pg
-- requires: roles
-- requires: schema_camper
-- requires: amenity
-- requires: amenity_carousel

begin;

set search_path to camper, public;

create or replace function add_amenity_carousel_slide(company_id integer, label text, media_id integer, caption text) returns integer as
$$
	insert into amenity_carousel (amenity_id, media_id, caption)
	select amenity_id, media_id, coalesce(caption, '')
	from amenity
	where label = add_amenity_carousel_slide.label
	  and company_id = add_amenity_carousel_slide.company_id
	union all
	select -1, 1, ''
	limit 1
	on conflict (amenity_id, media_id) do update
	set caption = excluded.caption
	returning amenity_id
	;
$$
	language sql
;

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

commit;