import { SharingPlaylogId, getSongsWithTitles } from "@/lib/api";
import { shareScore } from "../token";

export default async function Share({
  params,
}: {
  params: { token: string; id: string };
}) {
  const { token, id } = params;

  // Verify the token
  const tokenResult = await shareScore(token);

  if (tokenResult.error) {
    // Handle error appropriately, e.g., render an error message
    return <p>{tokenResult.error}</p>;
  }

  const userId = 10000;
  const playlogId = parseInt(id);

  const songsData = await getSongsWithTitles(userId);
  const playlogIds = await SharingPlaylogId(playlogId);

  const matchingSongs = songsData.filter((song) =>
    playlogIds.includes(song.id)
  );

  const songTitles = matchingSongs.map((song) => song.title);

  return (
    <ul>
      {songTitles.map((title) => (
        <li key={title}>{title}</li>
      ))}
    </ul>
  );
}
