import axios from 'axios'

const API_BASE = import.meta.env.VITE_API_URL || '/api'

const api = axios.create({
  baseURL: API_BASE,
  headers: {
    'Content-Type': 'application/json',
  },
  withCredentials: true,
})

export const mealPlannerApi = {
  auth: {
    login: (password: string) => api.post('/auth/login', { password }),
    logout: () => api.post('/auth/logout'),
  },

  profile: {
    get: () => api.get('/profile'),
    update: (data: any) => api.put('/profile', data),
    getMembers: () => api.get('/profile/members'),
    addMember: (data: any) => api.post('/profile/members', data),
    deleteMember: (id: string) => api.delete(`/profile/members/${id}`),
  },

  recipes: {
    list: (params?: any) => api.get('/recipes', { params }),
    recommended: (familyProfileId: string, limit?: number) =>
      api.get('/recipes/recommended', { params: { family_profile_id: familyProfileId, limit } }),
    get: (id: string) => api.get(`/recipes/${id}`),
    create: (data: any) => api.post('/recipes', data),
    delete: (id: string) => api.delete(`/recipes/${id}`),
    listIngredients: () => api.get('/ingredients?limit=500'),
    createIngredient: (data: any) => api.post('/ingredients', data),
  },

  meals: {
    getPlanned: (weekStart?: string) => api.get('/meals', { params: { week_start: weekStart } }),
    get: (id: string) => api.get(`/meals/${id}`),
    getItem: (id: string) => api.get(`/meals/items/${id}`),
    create: (data: any) => api.post('/meals', data),
    lock: (id: string) => api.post(`/meals/${id}/lock`),
    getVotePage: (itemId: string, token: string) => api.get(`/meals/items/${itemId}/vote/${token}`),
    submitVote: (itemId: string, token: string, data: any) => api.post(`/meals/items/${itemId}/vote/${token}`, data),
    swapItem: (itemId: string, newRecipeId: string) => api.post(`/meals/items/${itemId}/swap?new_recipe_id=${newRecipeId}`),
    moveItem: (itemId: string, newDayOfWeek: number, newMealType: string) => api.put(`/meals/items/${itemId}/move`, null, { params: { new_day_of_week: newDayOfWeek, new_meal_type: newMealType } }),
    approveItem: (itemId: string) => api.post(`/meals/items/${itemId}/approve`),
    denyItem: (itemId: string) => api.post(`/meals/items/${itemId}/deny`),
    deleteItem: (itemId: string) => api.delete(`/meals/items/${itemId}`),
    generateItem: (mealPlanId: string, dayOfWeek: number, mealType: string) =>
      api.post(`/meals/${mealPlanId}/generate-item`, null, { params: { day_of_week: dayOfWeek, meal_type: mealType } }),
  },

  pantry: {
    list: () => api.get('/pantry'),
    add: (data: any) => api.post('/pantry', data),
    update: (id: string, data: any) => api.put(`/pantry/${id}`, data),
    remove: (id: string) => api.delete(`/pantry/${id}`),
  },

  shoppingList: {
    get: (weekStart?: string) => api.get('/shopping-list', { params: { week_start: weekStart } }),
    getPrint: () => api.get('/shopping-list/print'),
  },

  admin: {
    triggerScrape: (source?: string, type?: string) => api.post('/admin/scrape', null, { params: { source, scrape_type: type } }),
    getLogs: (params?: any) => api.get('/admin/logs', { params }),
    getLog: (id: string) => api.get(`/admin/logs/${id}`),
    getEmailLogs: (params?: any) => api.get('/admin/email-logs', { params }),
    getMealPlans: (params?: any) => api.get('/admin/meal-plans', { params }),
    getStats: () => api.get('/admin/stats'),
    testEmail: (email: string) => api.post('/admin/test-email', null, { params: { email } }),
    triggerOrchestrate: (step: string, weekStart?: string) => api.post(`/orchestrate/${step}`, null, { params: { week_start: weekStart } }),
  },

  feedback: {
    get: (mealPlanItemId: string) => api.get(`/feedback/${mealPlanItemId}`),
    create: (data: any) => api.post('/feedback', data),
  },
}

export default api
