import { Song } from '../models/Song.js';
import { ApiError } from '../utils/ApiError.js';
import { asyncHandler } from '../utils/asyncHandler.js';

// GET /api/public/songs/:slug — only ever exposes published songs.
// Drafts 404 publicly even if the slug is guessed, so unfinished pages
// never leak before an admin hits "Publish".
export const getPublicSong = asyncHandler(async (req, res) => {
  const song = await Song.findBySlug(req.params.slug);

  if (!song || song.status !== 'published') {
    throw new ApiError(404, 'This release is not available');
  }

  res.json({
    success: true,
    song: {
      title: song.title,
      artist: song.artist,
      album: song.album,
      artwork_url: song.artwork_url,
      spotify_url: song.spotify_url,
      apple_music_url: song.apple_music_url,
      audiomack_url: song.audiomack_url,
      boomplay_url: song.boomplay_url,
      slug: song.slug,
      release_date: song.release_date,
      published_at: song.published_at,
      meta_pixel_id: song.meta_pixel_id,
      tiktok_pixel_id: song.tiktok_pixel_id,
      custom_head_script: song.custom_head_script,
    },
  });
});
