import { defineStore } from 'pinia';
import { ref } from 'vue';
import { albumService } from '../services/albumService';

export const useAlbumStore = defineStore('albums', () => {
  const albums = ref([]);
  const currentAlbum = ref(null);
  const loading = ref(false);
  const error = ref(null);
  const totalAlbums = ref(0);

  async function fetchAlbums(limit = 10, offset = 0) {
    loading.value = true;
    error.value = null;
    try {
      const response = await albumService.getAlbums(limit, offset);
      albums.value = response.data; // Assuming response.data is the list or { albums: [], total_count: ... }
      // Check if response has total_count structure
      if (response.data.albums) {
          albums.value = response.data.albums;
          totalAlbums.value = response.data.total_count;
      } else {
          // Fallback if structure is different
           albums.value = response.data;
      }
    } catch (err) {
      console.error('Error fetching albums:', err);
      error.value = err.response?.data?.detail || 'Failed to fetch albums';
    } finally {
      loading.value = false;
    }
  }

  async function createAlbum(data) {
    loading.value = true;
    error.value = null;
    try {
      await albumService.createAlbum(data);
      await fetchAlbums(); // Refresh list
      return true;
    } catch (err) {
      console.error('Error creating album:', err);
      error.value = err.response?.data?.detail || 'Failed to create album';
      return false;
    } finally {
      loading.value = false;
    }
  }

  async function fetchAlbum(id) {
    loading.value = true;
    error.value = null;
    currentAlbum.value = null;
    try {
      const response = await albumService.getAlbum(id);
      currentAlbum.value = response.data;
      return response.data;
    } catch (err) {
      console.error('Error fetching album:', err);
      error.value = err.response?.data?.detail || 'Failed to fetch album';
      return null;
    } finally {
      loading.value = false;
    }
  }

  async function updateAlbum(id, data) {
      loading.value = true;
      error.value = null;
      try {
          await albumService.updateAlbum(id, data);
          if (currentAlbum.value && currentAlbum.value.id === id) {
              await fetchAlbum(id);
          }
          await fetchAlbums();
          return true;
      } catch (err) {
          console.error('Error updating album:', err);
          error.value = err.response?.data?.detail || 'Failed to update album';
          return false;
      } finally {
          loading.value = false;
      }
  }

  async function deleteAlbum(id) {
    loading.value = true;
    error.value = null;
    try {
      await albumService.deleteAlbum(id);
      await fetchAlbums(); // Refresh list
      return true;
    } catch (err) {
      console.error('Error deleting album:', err);
      error.value = err.response?.data?.detail || 'Failed to delete album';
      return false;
    } finally {
      loading.value = false;
    }
  }

  async function addGenerationToAlbum(albumId, generationId) {
    loading.value = true;
    error.value = null;
    try {
      await albumService.addGenerationToAlbum(albumId, generationId);
      // Optionally refresh current album if it matches
      if (currentAlbum.value && currentAlbum.value.id === albumId) {
          await fetchAlbum(albumId);
      }
      return true;
    } catch (err) {
      console.error('Error adding generation to album:', err);
      error.value = err.response?.data?.detail || 'Failed to add generation to album';
      return false;
    } finally {
      loading.value = false;
    }
  }

  async function removeGenerationFromAlbum(albumId, generationId) {
    loading.value = true;
    error.value = null;
    try {
      await albumService.removeGenerationFromAlbum(albumId, generationId);
      if (currentAlbum.value && currentAlbum.value.id === albumId) {
          await fetchAlbum(albumId);
      }
      return true;
    } catch (err) {
      console.error('Error removing generation from album:', err);
      error.value = err.response?.data?.detail || 'Failed to remove generation from album';
      return false;
    } finally {
      loading.value = false;
    }
  }

  return {
    albums,
    currentAlbum,
    loading,
    error,
    totalAlbums,
    fetchAlbums,
    createAlbum,
    fetchAlbum,
    updateAlbum,
    deleteAlbum,
    addGenerationToAlbum,
    removeGenerationFromAlbum
  };
});
