create or replace function public.register_user(_email text, _password_hash text)
  returns setof public.user
  language plpgsql
as $function$
declare
  _user_uuid uuid;
begin
  insert into "user" (
    user_email,
    user_password_hash
  ) values (
    _email,
    _password_hash
  ) returning user_uuid into _user_uuid;

  return query select * from "user" where user_uuid = _user_uuid;
end; $function$;

create or replace function public.validate_session(_session_uuid uuid)
  returns setof public.v_session
  language plpgsql
as $function$
begin
  return query select * from v_session
    where session_uuid = _session_uuid
      and session_time_last_active + session_timeout_length < now();
end; $function$;

create or replace function public.login_user_session(_user_uuid uuid, _timeout_length interval, _ip_addr varchar(50), _user_agent varchar(500), _referer varchar(500), _link uuid)
  returns setof public.v_session
  language plpgsql
as $function$
declare
  _session_uuid uuid;
begin
  insert into "session" (
    session_user_uuid,
    session_timeout_length,
    session_ip_address,
    session_user_agent,
    session_referer,
    session_originating_link
  ) values (
    _user_uuid,
    _timeout_length,
    _ip_addr,
    _user_agent,
    _referer,
    _link
  ) returning session_uuid into _session_uuid;

  return query select * from public.validate_session(_session_uuid);
end; $function$;

create or replace function public.create_item(_name text, _urlslug citext, _description text, _price_cents integer, _published boolean)
  returns setof public.v_item
  language plpgsql
as $function$
declare
  _item_uuid uuid;
begin
  insert into "item" (
    item_name,
    item_urlslug,
    item_description,
    item_price_cents,
    item_published
  ) values (
    _name,
    _urlslug,
    _description,
    _price_cents,
    _published
  ) returning item_uuid into _item_uuid;

  return query select * from public.v_item where item_uuid = _item_uuid;
end; $function$;

create or replace function public.add_image_to_item(_item_uuid uuid, _large_file bytea, _thumb_file bytea, _mime_type varchar, _uploader_uuid uuid)
  returns setof public.v_item
  language plpgsql
as $function$
declare
  _image_uuid uuid;
begin
  insert into "image" (
    image_item_uuid,
    image_large_file,
    image_thumb_file,
    image_mime_type,
    image_uploader_uuid
  ) values (
    _item_uuid,
    _large_file,
    _thumb_file,
    _mime_type,
    _uploader_uuid
  ) returning image_uuid into _image_uuid;

  return query select * from public.v_item where item_uuid = _item_uuid;
end; $function$;

create or replace function public.set_featured_image(_item_uuid uuid, _image_uuid uuid)
  returns setof public.v_item
  language plpgsql
as $function$
begin
  -- Un-feature all other images
  update "image" set
    image_featured = false
  where image_item_uuid = _item_uuid;

  update "image" set
    image_featured = true
  where image_uuid = _image_uuid;

  return query select * from public.v_item where item_uuid = _item_uuid;
end; $function$;

create or replace function public.get_image_large(_image_uuid uuid)
  returns table (image_uuid uuid, image_mime_type varchar, image_file bytea)
  language plpgsql
as $function$
begin
  return query select 
      image_uuid,
      image_mime_type,
      image_large_file as image_file 
    from "image" 
    where "image".image_uuid = _image_uuid;
end; $function$;

create or replace function public.get_image_thumb(_image_uuid uuid)
  returns table (image_uuid uuid, image_mime_type varchar, image_file bytea)
  language plpgsql
as $function$
begin
  return query select 
      image_uuid,
      image_mime_type,
      image_large_thumb as image_file 
    from "image" 
    where "image".image_uuid = _image_uuid;
end; $function$;
