CREATE OR REPLACE FUNCTION public.insert_url_with_custom_artist(in_url text, in_artist_name text) RETURNS text
	LANGUAGE plpgsql STRICT
	AS $$
DECLARE
	ret_value integer;
	temp_artist_id integer;
	temp_folder_path text;
BEGIN
	PERFORM * FROM artist_website aw
	WHERE aw.url = in_url;

    IF FOUND THEN
        RAISE EXCEPTION 'url already exists';
    END IF;

	PERFORM * FROM artist a
	WHERE a.name = in_artist_name;

    IF NOT FOUND THEN
		INSERT INTO artist (name) VALUES (in_artist_name);
    END IF;

	SELECT artist.id INTO temp_artist_id FROM artist
	WHERE artist.name = in_artist_name;

	INSERT INTO artist_website (url, artist_id) VALUES (in_url, temp_artist_id);

	SELECT * INTO ret_value FROM set_website_name(in_url);
	IF ret_value <> 0 THEN
		DELETE FROM artist_website WHERE url = in_url;
		RAISE EXCEPTION 'could not set website_name';
	END IF;

	SELECT * INTO ret_value FROM set_folder_path(in_url);
	IF ret_value <> 0 THEN
		DELETE FROM artist_website WHERE url = in_url;
		RAISE EXCEPTION 'could not set folder_path';
	END IF;

	SELECT folder_path INTO temp_folder_path FROM artist_website WHERE url ILIKE in_url;
	
	RETURN concat(in_url, ' # ', temp_folder_path);
END;
$$;
