'use server'

import { revalidateTag } from 'next/cache'

export async function revalidateAlbums() {
  revalidateTag('albums')
}

export async function revalidateAlbum() {
  revalidateTag('album')
}

export async function revalidateSongs() {
  revalidateTag('songs')
}

export async function revalidateSong() {
  revalidateTag('song')
}

export async function getAlbums() {
  return fetch('https://api.anatid.net/album/', {
    method: "GET",
    headers: { "Content-Type": "application/json" },
    next: {
      revalidate: 300,
      tags: ['albums']
    }
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function getAlbum(id: number) {
  return fetch(`https://api.anatid.net/album/${id}`, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
    next: {
      revalidate: 300,
      tags: ['album']
    }
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function createAlbum(formData: FormData) {
  const title = formData.get('title');
  const artist = formData.get('artist');
  const genre = formData.get('genre');
  console.log(title, artist, genre);
  return fetch("https://api.anatid.net/album/", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      title: title,
      artist: artist,
      genre: genre,
    })
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

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/${id}`, {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
      title: title,
      artist: artist,
      genre: genre,
    })
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function deleteAlbum(id: number) {
  return fetch(`https://api.anatid.net/album/${id}`, {
    method: "DELETE",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
    })
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function getSongs() {
  return fetch("https://api.anatid.net/song/", {
    method: "GET",
    headers: { "Content-Type": "application/json" },
    next: {
      revalidate: 300,
      tags: ['songs']
    }
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function getSong(id: number) {
  return fetch(`https://api.anatid.net/song/${id}`, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
    next: {
      revalidate: 300,
      tags: ['song']
    }
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function createSong(formData: FormData) {
  const albumId = formData.get('albumId');
  const title = formData.get('title');
  const duration = formData.get('duration');
  const trackNumber = formData.get('trackNumber');
  return fetch("https://api.anatid.net/song/", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      title: title,
      duration: duration,
      trackNumber: trackNumber,
      albumId: albumId,
    })
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function updateSong(formData: FormData) {
  const id = formData.get('id');
  const albumId = formData.get('albumId');
  const title = formData.get('title');
  const duration = formData.get('duration');
  const trackNumber = formData.get('trackNumber');
  return fetch(`https://api.anatid.net/song/${id}`, {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: id,
      title: title,
      duration: duration,
      trackNumber: trackNumber,
      albumId: albumId,
    })
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}

export async function deleteSong(id: number) {
  return fetch(`https://api.anatid.net/song/${id}`, {
    method: "DELETE",
    headers: { "Content-Type": "application/json" }
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    return Promise.reject(response);
  }).catch(error => {
    return error.json();
  });
}
