"use client";

import React, { createContext, useContext, ReactNode, useState, useCallback } from "react";
import { useGetHomeAggregateRecommend } from "@/hooks/useHome";
import type { HomeRecommendV2Output } from "@/services/home/types";

interface HomeDataContextType {
  data: HomeRecommendV2Output | undefined;
  isLoading: boolean;
  error: Error | null;
  recommendExcludeList: number[];
  setRecommendExcludeList: (list: number[]) => void;
  isRecommendDataReady: boolean;
  setIsRecommendDataReady: (ready: boolean) => void;
}

const HomeDataContext = createContext<HomeDataContextType | undefined>(undefined);

export function HomeDataProvider({ children }: { children: ReactNode }) {
  const { data, isLoading, error } = useGetHomeAggregateRecommend();
  const [recommendExcludeList, setRecommendExcludeList] = useState<number[]>([]);
  const [isRecommendDataReady, setIsRecommendDataReady] = useState(false);
  
  return (
    <HomeDataContext.Provider value={{ 
      data, 
      isLoading, 
      error,
      recommendExcludeList,
      setRecommendExcludeList,
      isRecommendDataReady,
      setIsRecommendDataReady
    }}>
      {children}
    </HomeDataContext.Provider>
  );
}

export function useHomeData() {
  const context = useContext(HomeDataContext);
  if (context === undefined) {
    throw new Error("useHomeData must be used within a HomeDataProvider");
  }
  return context;
}

