'use server'

export async function getAlbums() {
  return fetch('https://api.anatid.net/album');
}

export async function getAlbum(id: number) {
  return fetch(`https://api.anatid.net/album/${id}`, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
  });
}

export async function createAlbum(formData: FormData) {
  const title = formData.get('title');
  const artist = formData.get('artist');
  const genre = formData.get('genre');
  return fetch("https://api.anatid.net/album", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      title: title,
      artist: artist,
      genre: genre,
    })
  });
}

export async function updateAlbum(formData: FormData) {
  const id = formData.get('id');
  const title = formData.get('title');
  const artist = formData.get('artist');
  const genre = formData.get('genre');
  return fetch("https://api.anatid.net/album", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
      title: title,
      artist: artist,
      genre: genre,
    })
  });
}

export async function deleteAlbum(formData: FormData) {
  const id = formData.get('id');
  return fetch("https://api.anatid.net/album", {
    method: "DELETE",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
    })
  });
}

export async function getSongs() {
  return fetch("https://api.anatid.net/song/");
}

export async function getSong(id: number) {
  return fetch(`https://api.anatid.net/song/${id}`, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
  });
}

export async function createSong(formData: FormData) {
  const albumId = formData.get('album-id');
  const title = formData.get('title');
  const duration = formData.get('duration');
  return fetch("https://api.anatid.net/song", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      albumId: albumId,
      title: title,
      duration: duration,
    })
  });
}

export async function updateSong(formData: FormData) {
  const id = formData.get('id');
  const albumId = formData.get('album-id');
  const title = formData.get('title');
  const duration = formData.get('duration');
  return fetch("https://api.anatid.net/song", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
      albumId: albumId,
      title: title,
      duration: duration,
    })
  });
}

export async function deleteSong(formData: FormData) {
  const id = formData.get('id');
  return fetch("https://api.anatid.net/song", {
    method: "DELETE",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
    })
  });
}
