import axios, { type AxiosError } from "axios";
import type { ApiResponse } from "./types";

const baseURL =
  process.env.NEXT_PUBLIC_ADMIN_API_URL ?? "http://localhost:5081";

export const adminApi = axios.create({
  baseURL,
  headers: { "Content-Type": "application/json" },
});

adminApi.interceptors.request.use((config) => {
  if (typeof window !== "undefined") {
    const token = localStorage.getItem("meezi_admin_access_token");
    if (token) config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

adminApi.interceptors.response.use(
  (response) => response,
  (error: AxiosError<ApiResponse<unknown>>) => {
    const apiError = error.response?.data?.error;
    if (apiError?.code) {
      return Promise.reject(new AdminApiClientError(apiError.code, apiError.message));
    }
    if (error.response?.status === 401 && typeof window !== "undefined") {
      localStorage.removeItem("meezi_admin_access_token");
      localStorage.removeItem("meezi_admin_refresh_token");
      localStorage.removeItem("meezi_admin_auth");
      const locale = window.location.pathname.split("/")[1] ?? "fa";
      window.location.href = `/${locale}/admin/login`;
    }
    return Promise.reject(error);
  }
);

export class AdminApiClientError extends Error {
  constructor(
    public readonly code: string,
    message: string
  ) {
    super(message);
    this.name = "AdminApiClientError";
  }
}

export async function adminGet<T>(url: string): Promise<T> {
  const { data } = await adminApi.get<ApiResponse<T>>(url);
  if (!data.success || data.data === undefined) {
    throw new Error(data.error?.message ?? "Request failed");
  }
  return data.data;
}

export async function adminPost<T>(url: string, body?: unknown): Promise<T> {
  const { data } = await adminApi.post<ApiResponse<T>>(url, body);
  if (!data.success || data.data === undefined) {
    throw new Error(data.error?.message ?? "Request failed");
  }
  return data.data;
}

export async function adminPut<T>(url: string, body: unknown): Promise<T> {
  const { data } = await adminApi.put<ApiResponse<T>>(url, body);
  if (!data.success || data.data === undefined) {
    throw new Error(data.error?.message ?? "Request failed");
  }
  return data.data;
}

export async function adminPatch<T>(url: string, body: unknown): Promise<T> {
  const { data } = await adminApi.patch<ApiResponse<T>>(url, body);
  if (!data.success || data.data === undefined) {
    throw new Error(data.error?.message ?? "Request failed");
  }
  return data.data;
}

export async function adminDelete(url: string): Promise<void> {
  const { data } = await adminApi.delete<ApiResponse<unknown>>(url);
  if (!data.success) {
    throw new Error(data.error?.message ?? "Request failed");
  }
}
