/**
 * preferenceStore — 本次注入偏好摘要
 * 飞轮隐形：只显注入提示，无"训练AI"按钮
 * 对应 GET /api/v1/tasks/{id}/preference/context
 */
import { create } from 'zustand';
import { PreferenceContext } from '@/types/index';
import { api } from '@/lib/api';

interface PreferenceState {
  context: PreferenceContext | null;
  isLoading: boolean;
  taskId: number | null;

  fetchContext: (taskId: number) => Promise<void>;
  clear: () => void;
}

export const usePreferenceStore = create<PreferenceState>((set) => ({
  context: null,
  isLoading: false,
  taskId: null,

  async fetchContext(taskId) {
    set({ isLoading: true, taskId });
    try {
      const data = await api.get<PreferenceContext>(
        `/api/v1/tasks/${taskId}/preference/context`
      );
      set({ context: data, isLoading: false });
    } catch {
      // 无偏好上下文不阻塞流程
      set({ isLoading: false });
    }
  },

  clear() {
    set({ context: null, taskId: null });
  },
}));
