import { fetchApi } from "@/lib/api/http";  // ✅ ADD
import type {
  Country,
  CreateCountryDto,
  UpdateCountryDto,
} from "@/lib/api/types/country.types";

export const COUNTRIES_API_BASE_URL =
  process.env.NEXT_PUBLIC_API_BASE_URL ?? "http://localhost:3007/api";

// ✅ DELETE local fetchApi entirely

export async function getCountries(): Promise<Country[]> {
  return fetchApi<Country[]>(`${COUNTRIES_API_BASE_URL}/countries`);
}

export async function getCountry(id: number): Promise<Country> {
  return fetchApi<Country>(`${COUNTRIES_API_BASE_URL}/countries/${id}`);
}

export async function createCountry(dto: CreateCountryDto): Promise<Country> {
  return fetchApi<Country>(`${COUNTRIES_API_BASE_URL}/countries`, {
    method: "POST",
    body: JSON.stringify(dto),
  });
}

export async function updateCountry(id: number, dto: UpdateCountryDto): Promise<Country> {
  return fetchApi<Country>(`${COUNTRIES_API_BASE_URL}/countries/${id}`, {
    method: "PUT",
    body: JSON.stringify(dto),
  });
}

export async function deleteCountry(id: number): Promise<void> {
  return fetchApi<void>(`${COUNTRIES_API_BASE_URL}/countries/${id}`, {
    method: "DELETE",
  });
}