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

export const useIdeaStore = defineStore('ideas', () => {
  const ideas = ref([]);
  const currentIdea = ref(null);
  const loading = ref(false);
  const error = ref(null);
  const totalIdeas = ref(0);

  async function fetchIdeas(limit = 10, offset = 0) {
    loading.value = true;
    error.value = null;
    try {
      const response = await ideaService.getIdeas(limit, offset);
      if (response.data.ideas) {
          ideas.value = response.data.ideas;
          totalIdeas.value = response.data.total_count;
      } else {
          ideas.value = response.data;
      }
    } catch (err) {
      console.error('Error fetching ideas:', err);
      error.value = err.response?.data?.detail || 'Failed to fetch ideas';
    } finally {
      loading.value = false;
    }
  }

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

  async function fetchIdea(id) {
    loading.value = true;
    error.value = null;
    currentIdea.value = null;
    try {
      const response = await ideaService.getIdea(id);
      currentIdea.value = response.data;
      return response.data;
    } catch (err) {
      console.error('Error fetching idea:', err);
      error.value = err.response?.data?.detail || 'Failed to fetch idea';
      return null;
    } finally {
      loading.value = false;
    }
  }

  async function updateIdea(id, data) {
      loading.value = true;
      error.value = null;
      try {
          await ideaService.updateIdea(id, data);
          if (currentIdea.value && currentIdea.value.id === id) {
              await fetchIdea(id);
          }
          await fetchIdeas();
          return true;
      } catch (err) {
          console.error('Error updating idea:', err);
          error.value = err.response?.data?.detail || 'Failed to update idea';
          return false;
      } finally {
          loading.value = false;
      }
  }

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

  async function addGenerationToIdea(ideaId, generationId) {
    loading.value = true;
    error.value = null;
    try {
      await ideaService.addGenerationToIdea(ideaId, generationId);
      if (currentIdea.value && currentIdea.value.id === ideaId) {
          await fetchIdea(ideaId);
      }
      return true;
    } catch (err) {
      console.error('Error adding generation to idea:', err);
      error.value = err.response?.data?.detail || 'Failed to add generation to idea';
      return false;
    } finally {
      loading.value = false;
    }
  }

  async function removeGenerationFromIdea(ideaId, generationId) {
    loading.value = true;
    error.value = null;
    try {
      await ideaService.removeGenerationFromIdea(ideaId, generationId);
      if (currentIdea.value && currentIdea.value.id === ideaId) {
          await fetchIdea(ideaId);
      }
      return true;
    } catch (err) {
      console.error('Error removing generation from idea:', err);
      error.value = err.response?.data?.detail || 'Failed to remove generation from idea';
      return false;
    } finally {
      loading.value = false;
    }
  }

  // Assuming getIdeaGenerations is separate from getIdea
  async function fetchIdeaGenerations(ideaId, limit = 100, offset = 0) {
      try {
          const response = await ideaService.getIdeaGenerations(ideaId, limit, offset);
          return response;
      } catch (err) {
          console.error('Error fetching idea generations:', err);
          return { data: [] };
      }
  }

  return {
    ideas,
    currentIdea,
    loading,
    error,
    totalIdeas,
    fetchIdeas,
    createIdea,
    fetchIdea,
    updateIdea,
    deleteIdea,
    addGenerationToIdea,
    removeGenerationFromIdea,
    fetchIdeaGenerations
  };
});
