import { baseAPI } from "@/Shared/API/baseAPI";
import { IFetch, IList } from "@/Shared/types";
import axios from "axios";
import { StaticImageData } from "next/image";
import { create } from "zustand";

interface INews extends IList {
  results: IResult[];
}

interface IResult {
  id: number;
  image: StaticImageData;
  title: string;
  description: string;
  created_at: string;
}

interface INewsStore extends IFetch {
  data: INews;
  getNews: () => Promise<void>;
}

export const useNews = create<INewsStore>((set) => ({
  data: {
    count: 0,
    next: null,
    previous: null,
    results: [],
  },
  loading: false,
  error: "",
  getNews: async () => {
    try {
      set({ loading: true });
      const response = await axios.get<INews>(`${baseAPI}/news/`);

      set({ data: response.data });
    } catch (error: any) {
      set({ error: error.message });
    } finally {
      set({ loading: false });
    }
  },
}));
