// MusicRoom - Init module
// Application initialization sequence

(function() {
  const M = window.MusicRoom;
  
  // Fetch server status/config
  async function loadServerStatus() {
    try {
      const res = await fetch("/api/status");
      M.serverStatus = await res.json();
      console.log("Server status:", M.serverStatus);
    } catch (e) {
      console.warn("Failed to load server status");
      M.serverStatus = null;
    }
  }
  
  // Initialize track storage
  async function initStorage() {
    await TrackStorage.init();
    await M.updateCacheStatus();
    console.log(`TrackStorage: ${M.cachedTracks.size} tracks cached`);
  }
  
  // Setup history panel handlers
  document.addEventListener("DOMContentLoaded", () => {
    const btnHistory = M.$("#btn-history");
    const btnClose = M.$("#btn-close-history");
    
    if (btnHistory) {
      btnHistory.onclick = () => M.toggleToastHistory();
    }
    if (btnClose) {
      btnClose.onclick = () => M.toggleToastHistory();
    }
  });
  
  // Initialize the application
  Promise.all([initStorage(), loadServerStatus()]).then(async () => {
    await M.loadLibrary();
    await M.loadCurrentUser();
    if (M.currentUser) {
      M.loadChannels();
    }
  });
})();
