/**
 * API Client Factory
 * Centralized factory for creating configured API client instances
 * Ensures consistent configuration across all API calls
 */

import { getApiBaseUrl } from "./api-config";
import { createAuthErrorMiddleware } from "./api-error-handler";
import {
  Configuration,
  UserManagementApi,
  ProductManagementApi,
  OrderManagementApi,
  ShoppingCartApi,
  ChatApi,
  WalletManagementApi,
  DiscountManagementApi,
  MediaManagementApi,
  ShippingManagementApi,
  CourierManagementApi,
  SellerCourierManagementApi,
  SearchSystemApi,
  FollowsManagementApi,
  CommentReviewManagmentApi,
  HomepageApi,
  ServiceProductApi,
  ServiceUserApi,
  ApiApi,
  SellerManagementApi,
  BookmarkApi,
  InstagramManagementApi,
  CollectionsAdminApi,
  CollectionsPublicApi,
  CollectionsApi,
  AIFitApi,
} from "../../src/api/types";

/**
 * Base configuration factory with authentication support
 */
export function createApiConfiguration(token?: string): Configuration {
  const baseUrl = getApiBaseUrl();

  const config = new Configuration({
    basePath: baseUrl,
    headers: {
      "Content-Type": "application/json",
      ...(token && { Authorization: `Bearer ${token}` }),
    },
    middleware: [createAuthErrorMiddleware()],
  });

  return config;
}

/**
 * API client factory functions
 * Each function creates a properly configured API client instance
 */

export const createUserManagementApi = (token?: string) =>
  new UserManagementApi(createApiConfiguration(token));

export const createProductManagementApi = (token?: string) =>
  new ProductManagementApi(createApiConfiguration(token));

export const createOrderManagementApi = (token?: string) =>
  new OrderManagementApi(createApiConfiguration(token));

export const createShoppingCartApi = (token?: string) =>
  new ShoppingCartApi(createApiConfiguration(token));

export const createChatApi = (token?: string) =>
  new ChatApi(createApiConfiguration(token));

export const createWalletManagementApi = (token?: string) =>
  new WalletManagementApi(createApiConfiguration(token));

export const createDiscountManagementApi = (token?: string) =>
  new DiscountManagementApi(createApiConfiguration(token));

export const createMediaManagementApi = (token?: string) =>
  new MediaManagementApi(createApiConfiguration(token));

export const createShippingManagementApi = (token?: string) =>
  new ShippingManagementApi(createApiConfiguration(token));

export const createCourierManagementApi = (token?: string) =>
  new CourierManagementApi(createApiConfiguration(token));

export const createSellerCourierManagementApi = (token?: string) =>
  new SellerCourierManagementApi(createApiConfiguration(token));

export const createSearchSystemApi = (token?: string) =>
  new SearchSystemApi(createApiConfiguration(token));

export const createFollowsManagementApi = (token?: string) =>
  new FollowsManagementApi(createApiConfiguration(token));

export const createCommentReviewManagmentApi = (token?: string) =>
  new CommentReviewManagmentApi(createApiConfiguration(token));

export const createHomepageApi = (token?: string) =>
  new HomepageApi(createApiConfiguration(token));

export const createServiceProductApi = (token?: string) =>
  new ServiceProductApi(createApiConfiguration(token));

export const createServiceUserApi = (token?: string) =>
  new ServiceUserApi(createApiConfiguration(token));

export const createApiApi = (token?: string) =>
  new ApiApi(createApiConfiguration(token));

export const createSellerManagementApi = (token?: string) =>
  new SellerManagementApi(createApiConfiguration(token));

export const createBookmarkApi = (token?: string) =>
  new BookmarkApi(createApiConfiguration(token));

export const createInstagramManagementApi = (token?: string) =>
  new InstagramManagementApi(createApiConfiguration(token));

export const createCollectionsAdminApi = (token?: string) =>
  new CollectionsAdminApi(createApiConfiguration(token));

export const createCollectionsPublicApi = (token?: string) =>
  new CollectionsPublicApi(createApiConfiguration(token));

export const createCollectionsApi = (token?: string) =>
  new CollectionsApi(createApiConfiguration(token));

export const createAIFitApi = (token?: string) =>
  new AIFitApi(createApiConfiguration(token));

/**
 * Convenience function to create all API clients at once
 * Useful for dependency injection or when you need multiple APIs
 */
export function createAllApiClients(token?: string) {
  const config = createApiConfiguration(token);

  return {
    userManagement: new UserManagementApi(config),
    productManagement: new ProductManagementApi(config),
    orderManagement: new OrderManagementApi(config),
    shoppingCart: new ShoppingCartApi(config),
    chat: new ChatApi(config),
    walletManagement: new WalletManagementApi(config),
    discountManagement: new DiscountManagementApi(config),
    mediaManagement: new MediaManagementApi(config),
    shippingManagement: new ShippingManagementApi(config),
    courierManagement: new CourierManagementApi(config),
    sellerCourierManagement: new SellerCourierManagementApi(config),
    searchSystem: new SearchSystemApi(config),
    followsManagement: new FollowsManagementApi(config),
    commentReviewManagement: new CommentReviewManagmentApi(config),
    homepage: new HomepageApi(config),
    serviceProduct: new ServiceProductApi(config),
    serviceUser: new ServiceUserApi(config),
    api: new ApiApi(config),
  };
}

/**
 * Type for the complete API client collection
 */
export type ApiClients = ReturnType<typeof createAllApiClients>;
