<script lang="ts">
    import type { Suggestion } from "$lib/types"

    let { suggestions, roomId }: { suggestions: Suggestion[]; roomId: string } = $props()

    async function vote(idx: number, amount: number, songId: string) {
        suggestions[idx].upvote += amount
        await fetch(`/api/song/voting?room=${roomId}&song=${songId}&increment=${amount}`, { method: "POST" })
    }
</script>

<div class="flex h-full w-full flex-col items-center gap-2 overflow-y-auto">
    {#if suggestions.length == 0}
        <p>No suggestions yet! Try to add a new one using the <b>Add</b> button</p>
    {/if}

    {#each suggestions as sug, idx}
        <div class="shadow-md hover:bg-indigo-400 duration-100 bg-indigo-500 flex h-[80px] w-full flex-row gap-2 rounded border-2 border-indigo-600 p-2">
            <div class="flex w-3/4 flex-row gap-2">
                <img class="w-[60px] min-w-[60px] rounded" src={`https://lastfm.freetls.fastly.net/i/u/174s/${sug.image_id}.png`} alt="Song cover" />
                <div class="text-white">
                    <p>{sug.title}</p>
                    <p>{sug.artist}</p>
                </div>
            </div>
            <div class="flex w-1/4 flex-row items-center justify-center gap-2">
                <button
                    class="grayscale"
                    onclick={() => {
                        vote(idx, 1, sug.uuid)
                    }}>👍</button
                >
                <p class="text-white font-semibold">{sug.upvote}</p>
                <button
                    class="hover:scale-150 duration-100"
                    onclick={() => {
                        vote(idx, -1, sug.uuid)
                    }}><div class="rotate-180">👍</div></button
                >
            </div>
        </div>
    {/each}
</div>
