import axios from "axios";
import { Article } from "../domain/articleEntity";
import { create } from "../domain/articleModel";
import { FetchArticleByIdDTO } from "./dto/fetch_article_by_id_dto";
import Failure from "core/failure";
import { integratorApiClient } from "core/httpClient";

const articleEndpoint = "/papers/"

async function getArticle(id: string): Promise<Article> {
  try {
    const response = await integratorApiClient.get<FetchArticleByIdDTO>(
      // `https://run.mocky.io/v3/62cd4581-d864-4d46-b1d6-02b45b5d1994/${id}`
      // `https://jsonplaceholder.typicode.com/posts/${id}`
      `https://run.mocky.io/v3/066be3d8-0568-439a-8b20-062deed49a97`
      // articleEndpoint + id
    );
    const dto = response.data;
    return create({
      id: dto.id,
      topic: [dto.topic],
      title: dto.title,
      authors: dto.authors,
      tags: dto.tags,
      summary: dto.summary,
      content: dto.content,
    });
  } catch (reason) {
    if (axios.isAxiosError(reason)) {
      throw Failure.fromReason(reason, "failures.services.load");
    }
    throw reason;
  }
}

export { getArticle };
