import { baseAPI } from "@/Shared/API/baseAPI";
import { IList } from "@/Shared/types";
import axios, { AxiosError } from "axios";

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

interface IResult {
  id: number;
  author: IAuthor;
  review: string;
  created_at: string;
}

interface IAuthor {
  id: number;
  first_name: string;
  last_name: string;
  image: string;
}

class NewsReviewsStore {
  error: string;
  constructor() {
    this.error = "";
  }

  async getReviews(endpoint: string, id: number | null | undefined) {
    try {
      const response = await axios.get<IReview>(
        `${baseAPI}/${endpoint}/${id}/reviews/`
      );

      return response.data;
    } catch (error: unknown) {
      if (error instanceof AxiosError) {
        this.error = error.message;
      } else {
        this.error = "An error ocured";
      }
    }
  }
}

export const newsReviewStore = new NewsReviewsStore();
