import { playlistsRef } from "../../../config/firebase";

export const createPlaylist = async (payload = {}) => {
  try {
    const createdBy = payload?.createdBy;
    const name = payload?.name?.trim?.();
    const musics = Array.isArray(payload?.musics) ? payload.musics : [];

    const doc = {
      createdBy,
      name,
      musics,
      createdAt: new Date(),
      updatedAt: new Date(),
    };

    const docRef = await playlistsRef.add(doc);
    return docRef.id;
  } catch (error) {
    console.error("Error creating playlist:", error);
    throw error;
  }
};
