import { defineStore } from 'pinia'
import { ref } from 'vue'
import { api } from '../composables/useApi'

export const EDITOR_ONLY_TAGS = ['已审核']

export const useRecipesStore = defineStore('recipes', () => {
  const recipes = ref([])
  const allTags = ref([])
  const userFavorites = ref([])

  // Actions
  async function loadRecipes() {
    const data = await api.get('/api/recipes')
    recipes.value = data.map((r) => ({
      _id: r._id ?? r.id,
      _owner_id: r._owner_id ?? r.owner_id,
      _owner_name: r._owner_name ?? r.owner_name ?? '',
      _version: r._version ?? r.version ?? 1,
      name: r.name,
      en_name: r.en_name ?? '',
      note: r.note ?? '',
      tags: r.tags ?? [],
      ingredients: (r.ingredients ?? []).map((ing) => ({
        oil: ing.oil_name ?? ing.oil ?? ing.name,
        drops: ing.drops,
      })),
    }))
  }

  async function loadTags() {
    const data = await api.get('/api/tags')
    const apiTags = data.map((t) => (typeof t === 'string' ? t : t.name))
    const recipeTags = recipes.value.flatMap((r) => r.tags)
    const tagSet = new Set([...apiTags, ...recipeTags])
    allTags.value = [...tagSet].sort((a, b) => a.localeCompare(b, 'zh'))
  }

  async function loadFavorites() {
    try {
      const data = await api.get('/api/favorites')
      userFavorites.value = data.map((f) => f._id ?? f.id ?? f.recipe_id ?? f)
    } catch {
      userFavorites.value = []
    }
  }

  async function saveRecipe(recipe) {
    if (recipe._id) {
      const data = await api.put(`/api/recipes/${recipe._id}`, recipe)
      // Reload from server to get properly formatted data (oil_name → oil mapping)
      await loadRecipes()
      return data
    } else {
      const data = await api.post('/api/recipes', recipe)
      // Refresh list; if refresh fails, still return success (recipe was saved)
      try {
        await loadRecipes()
      } catch (e) {
        console.warn('[saveRecipe] loadRecipes failed after save:', e)
      }
      return data
    }
  }

  async function deleteRecipe(id) {
    await api.delete(`/api/recipes/${id}`)
    recipes.value = recipes.value.filter((r) => r._id !== id)
  }

  async function toggleFavorite(recipeId) {
    if (userFavorites.value.includes(recipeId)) {
      await api.delete(`/api/favorites/${recipeId}`)
      userFavorites.value = userFavorites.value.filter((id) => id !== recipeId)
    } else {
      await api.post(`/api/favorites/${recipeId}`)
      userFavorites.value.push(recipeId)
    }
  }

  function isFavorite(recipe) {
    return userFavorites.value.includes(recipe._id)
  }

  async function createTag(name) {
    await api.post('/api/tags', { name })
    if (!allTags.value.includes(name)) {
      allTags.value = [...allTags.value, name].sort((a, b) => a.localeCompare(b, 'zh'))
    }
  }

  async function deleteTag(name) {
    await api.delete(`/api/tags/${encodeURIComponent(name)}`)
    allTags.value = allTags.value.filter((t) => t !== name)
  }

  return {
    recipes,
    allTags,
    userFavorites,
    loadRecipes,
    loadTags,
    loadFavorites,
    saveRecipe,
    deleteRecipe,
    toggleFavorite,
    isFavorite,
    createTag,
    deleteTag,
  }
})
