import streamlit as st from .enums import Page from .utils import artist_select from ..assets.models import GlobalParticipant from ..assets.services import AssetService # Import the mock service and models (replace with your actual imports) def clear_artist_inputs(): st.session_state.selected_artist = None st.session_state.new_artist_name = "" def render_add_song_page(asset_service: AssetService): artists_by_name: dict[str, GlobalParticipant] = st.session_state.artist_by_name """Renders the page for adding a new song.""" st.title("🎵 Add a New Song") st.caption( "Add a new song to the library by providing an artist and a YouTube URL." ) if st.button("⬅️ Back to Search"): st.session_state.page = Page.ARTIST_SEARCH st.rerun() st.markdown("---") st.subheader("Step 1: Choose the Artist") st.radio( "Is the artist already in the library?", ("Select an existing artist", "Add a new artist"), horizontal=True, label_visibility="collapsed", key="artist_choice_radio", on_change=clear_artist_inputs, width="stretch", ) with st.form(key="add_song_form", border=False): artist = None artist_name = "" if st.session_state.artist_choice_radio == "Select an existing artist": artist_select(list(artists_by_name.keys())) else: st.text_input( "New Artist Name", placeholder="Enter the artist's name", key="new_artist_name", ) artist_name = "" if st.session_state.artist_choice_radio == "Select an existing artist": if st.session_state.selected_artist: artist_name = st.session_state.selected_artist artist = st.session_state.artist_by_name.get(artist_name, None) else: if st.session_state.new_artist_name: artist_name = st.session_state.new_artist_name.strip() st.subheader("Step 2: Provide Song Details") song_name = st.text_input("Song Name", placeholder="e.g., 'Sunset'") youtube_url = st.text_input( "YouTube URL", placeholder="https://www.youtube.com/watch?v=..." ) st.markdown("---") submitted = st.form_submit_button( "Save Song & Clip It", use_container_width=True ) if submitted: is_valid = True if not artist_name: st.error("Please select or add an artist.") is_valid = False if not song_name.strip(): st.error("Please enter a song name.") is_valid = False if is_valid: with st.spinner(f"Processing '{song_name}'... This may take a moment."): try: st.session_state.artist_name = artist_name asset_service.add_song( song_name=song_name, key=youtube_url, artist=artist, artist_name=artist_name, ) st.session_state.page = Page.CLIP_IT st.toast(f"'{song_name}' added successfully!", icon="✅") st.rerun() except Exception as e: st.error(f"An error occurred: {e}")