import { QueryBIRepository } from "@/domain/repositories/QueryBiRepository.js";
import { api } from "@/infra/api.js";
import { ApiResponseData } from "@/domain/entities/ApiResponseData.js";

export class QueryBiRepositoryImp implements QueryBIRepository {

  async queryBi(bi_number: string): Promise<ApiResponseData> {
    try {

      const response = await api.get("/actions/bi.ajcall.php", {
        params: {
          bi: bi_number
        },
        headers: {
          "Accept": process.env.HTTP_ACCEPT || "application/json, text/javascript, */*; q=0.01",
          "Referer": process.env.HTTP_REFERER || "https://sme.gov.ao",
          "X-Requested-With": process.env.HTTP_X_REQUESTED_WITH || "XMLHttpRequest"
        }
      });

      const FetchData = response.data.data;

      return new ApiResponseData(
        FetchData.numero,
        FetchData.nome_completo,
        FetchData.nome,
        FetchData.apelido,
        FetchData.nif,
        FetchData.data_nasc,
        FetchData.genero,
        FetchData.naturalidade,
        FetchData.nacionalidade_nome,
        FetchData.pai_nome_completo,
        FetchData.mae_nome_completo,
        FetchData.estado_civil,
        FetchData.data_emissao,
        FetchData.emissao_local
      );

    } catch (error) {
      console.error("Erro ao consultar BI:", error);

      throw new Error(
        error instanceof Error
          ? error.message
          : "Something went wrong"
      );
    }
  }
}