let albums = [
  {
    id: crypto.randomUUID(),
    title: "Soundtracks for the Blind",
    artist: "Swans",
    year: "1996",
    genre: "Experimental Rock",
    cover: null,
    tracks: [{ name: "Red Velvet Corridor.mp3" }, { name: "Helpless Child.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Random Access Memories",
    artist: "Daft Punk",
    year: "2013",
    genre: "Electronic",
    cover: null,
    tracks: [{ name: "Give Life Back to Music.mp3" }, { name: "Get Lucky.mp3" }],
  },
  {
    id: crypto.randomUUID(),
    title: "In Rainbows",
    artist: "Radiohead",
    year: "2007",
    genre: "Alternative Rock",
    cover: null,
    tracks: [{ name: "15 Step.mp3" }, { name: "Nude.flac" }, { name: "Reckoner.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Kind of Blue",
    artist: "Miles Davis",
    year: "1959",
    genre: "Jazz",
    cover: null,
    tracks: [{ name: "So What.mp3" }, { name: "Freddie Freeloader.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Discovery",
    artist: "Daft Punk",
    year: "2001",
    genre: "House",
    cover: null,
    tracks: [{ name: "One More Time.mp3" }, { name: "Digital Love.mp3" }],
  },
  {
    id: crypto.randomUUID(),
    title: "To Pimp a Butterfly",
    artist: "Kendrick Lamar",
    year: "2015",
    genre: "Hip-Hop",
    cover: null,
    tracks: [{ name: "Wesley's Theory.mp3" }, { name: "Alright.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Vespertine",
    artist: "Björk",
    year: "2001",
    genre: "Art Pop",
    cover: null,
    tracks: [{ name: "Hidden Place.flac" }, { name: "Pagan Poetry.mp3" }],
  },
  {
    id: crypto.randomUUID(),
    title: "The Dark Side of the Moon",
    artist: "Pink Floyd",
    year: "1973",
    genre: "Progressive Rock",
    cover: null,
    tracks: [{ name: "Time.mp3" }, { name: "Money.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Dummy",
    artist: "Portishead",
    year: "1994",
    genre: "Trip Hop",
    cover: null,
    tracks: [{ name: "Mysterons.mp3" }, { name: "Sour Times.mp3" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Titanic Rising",
    artist: "Weyes Blood",
    year: "2019",
    genre: "Baroque Pop",
    cover: null,
    tracks: [{ name: "Andromeda.mp3" }, { name: "Movies.flac" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Selected Ambient Works 85-92",
    artist: "Aphex Twin",
    year: "1992",
    genre: "Ambient",
    cover: null,
    tracks: [{ name: "Xtal.wav" }, { name: "Tha.mp3" }],
  },
  {
    id: crypto.randomUUID(),
    title: "A Love Supreme",
    artist: "John Coltrane",
    year: "1965",
    genre: "Jazz",
    cover: null,
    tracks: [{ name: "Acknowledgement.mp3" }, { name: "Psalm.flac" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Madvillainy",
    artist: "Madvillain",
    year: "2004",
    genre: "Hip-Hop",
    cover: null,
    tracks: [{ name: "Accordion.mp3" }, { name: "All Caps.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Loveless",
    artist: "My Bloody Valentine",
    year: "1991",
    genre: "Shoegaze",
    cover: null,
    tracks: [{ name: "Only Shallow.mp3" }, { name: "When You Sleep.flac" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Igor",
    artist: "Tyler, The Creator",
    year: "2019",
    genre: "Hip-Hop",
    cover: null,
    tracks: [{ name: "Earfquake.mp3" }, { name: "A Boy Is a Gun.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Blackstar",
    artist: "David Bowie",
    year: "2016",
    genre: "Art Rock",
    cover: null,
    tracks: [{ name: "Blackstar.mp3" }, { name: "Lazarus.flac" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Punisher",
    artist: "Phoebe Bridgers",
    year: "2020",
    genre: "Indie Folk",
    cover: null,
    tracks: [{ name: "Garden Song.mp3" }, { name: "Kyoto.wav" }],
  },
  {
    id: crypto.randomUUID(),
    title: "Kid A",
    artist: "Radiohead",
    year: "2000",
    genre: "Experimental",
    cover: null,
    tracks: [{ name: "Everything In Its Right Place.mp3" }, { name: "Idioteque.flac" }],
  },
];

let users = [
  { id: crypto.randomUUID(), email: "admin@example.com" },
  { id: crypto.randomUUID(), email: "listener@example.com" },
];

export const api = {
  getAlbums() {
    return [...albums];
  },
  addAlbum(payload) {
    const album = {
      id: crypto.randomUUID(),
      title: payload.title,
      artist: payload.artist,
      year: payload.year,
      genre: payload.genre,
      cover: payload.cover || null,
      tracks: payload.tracks || [],
    };
    albums.unshift(album);
    return album;
  },
  deleteAlbum(id) {
    albums = albums.filter((a) => a.id !== id);
    return { ok: true };
  },
  getAlbumById(id) {
    return albums.find((a) => a.id === id) || null;
  },
  addTrack(albumId, track) {
    albums = albums.map((a) =>
      a.id === albumId
        ? { ...a, tracks: [...(a.tracks || []), { name: track.name }] }
        : a
    );
    return albums.find((a) => a.id === albumId) || null;
  },
  updateTrack(albumId, trackIndex, name) {
    albums = albums.map((a) => {
      if (a.id !== albumId) return a;
      const tracks = [...(a.tracks || [])];
      if (trackIndex < 0 || trackIndex >= tracks.length) return a;
      tracks[trackIndex] = { ...tracks[trackIndex], name };
      return { ...a, tracks };
    });
    return albums.find((a) => a.id === albumId) || null;
  },
  deleteTrack(albumId, trackIndex) {
    albums = albums.map((a) => {
      if (a.id !== albumId) return a;
      const tracks = [...(a.tracks || [])];
      if (trackIndex < 0 || trackIndex >= tracks.length) return a;
      tracks.splice(trackIndex, 1);
      return { ...a, tracks };
    });
    return albums.find((a) => a.id === albumId) || null;
  },
  moveTrack(albumId, fromIndex, toIndex) {
    albums = albums.map((a) => {
      if (a.id !== albumId) return a;
      const tracks = [...(a.tracks || [])];
      if (
        fromIndex < 0 ||
        fromIndex >= tracks.length ||
        toIndex < 0 ||
        toIndex >= tracks.length
      ) {
        return a;
      }
      const [moved] = tracks.splice(fromIndex, 1);
      tracks.splice(toIndex, 0, moved);
      return { ...a, tracks };
    });
    return albums.find((a) => a.id === albumId) || null;
  },
  getUsers() {
    return [...users];
  },
  updateUserEmail(id, email) {
    users = users.map((u) => (u.id === id ? { ...u, email } : u));
    return users.find((u) => u.id === id);
  },
  deleteUser(id) {
    users = users.filter((u) => u.id !== id);
    return { ok: true };
  },
};