export const validateSunoUrl = (url) => {
  const cleanUrl = url.trim().replace(/^@/, '');
  const sunoRegex = /^https?:\/\/(www\.)?suno\.com\/song\/[a-zA-Z0-9-]+$/;
  return sunoRegex.test(cleanUrl);
};

export const extractSongId = (url) => {
  const cleanUrl = url.trim().replace(/^@/, '');
  const match = cleanUrl.match(/song\/([a-zA-Z0-9-]+)$/);
  return match ? match[1] : null;
};

export const getFansNeededForNextLevel = (currentFans, currentLevel) => {
  const thresholds = [50, 250, 500, 1000];
  const currentThreshold = thresholds[currentLevel - 1];
  
  if (!currentThreshold) return 0; // Max level reached
  
  return Math.max(0, currentThreshold - currentFans);
};

export const seededRandom = (seed) => {
  let x = Math.sin(seed) * 10000;
  return x - Math.floor(x);
};

export const generateRoomSeed = (roomX, roomY) => {
  return roomX * 1000 + roomY * 100 + 42;
};

export const getAvailableCredits = (player) => {
  return Math.max(0, player.totalCreditsEarned - player.creditsSpent);
};

export const levelThresholds = [
  { level: 1, name: 'Aspiring Artist', requiredFans: 0, requiredStreams: 0 },
  { level: 2, name: 'Bedroom Producer', requiredFans: 50, requiredStreams: 100 },
  { level: 3, name: 'Local Performer', requiredFans: 250, requiredStreams: 500 },
  { level: 4, name: 'Rising Star', requiredFans: 500, requiredStreams: 1000 },
  { level: 5, name: 'Chart Climber', requiredFans: 1000, requiredStreams: 2000 }
];

export const calculateLevelFromStats = (fans, streams) => {
  for (let i = levelThresholds.length - 1; i >= 0; i--) {
    const threshold = levelThresholds[i];
    if (fans >= threshold.requiredFans && streams >= threshold.requiredStreams) {
      return threshold;
    }
  }
  return levelThresholds[0]; // Default to level 1
};