import { baseAPI } from "@/Shared/API/baseAPI";
import axios, { AxiosError } from "axios";
import { StaticImport } from "next/dist/shared/lib/get-img-props";
import { StaticImageData } from "next/image";

interface IDetails {
  id: number | null;
  image: string;
  title: string;
  description: string;
  news_review: IReview[];
  created_at: string;
  count_reviews: number;
}

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

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

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

  async getNewsDetails(id: string) {
    try {
      const response = await axios.get<IDetails>(
        `${baseAPI}/news/${id}/`
      );

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

export const newsDetailsStore = new NewsDetailsStore();
