import { apiEndpoints } from "@/lib/api/endpoints";
import { fetchWithAuth } from "@/lib/auth/client";
import type {
  MarketplaceHomeResponse,
  MarketplaceListing,
  MarketplaceRepairShop,
  MarketplaceRepairShopResponse,
  MarketplaceResponse,
  MarketplaceShopProfile,
  SuccessMessage,
} from "@/types/api";

type MarketplaceFormValue =
  | string
  | number
  | boolean
  | File
  | null
  | undefined
  | Array<string | number | boolean | File>;

type MarketplaceFormPayload = Record<string, MarketplaceFormValue>;

function appendFormValue(formData: FormData, key: string, value: MarketplaceFormValue) {
  if (value === undefined || value === null || value === "") {
    return;
  }

  if (Array.isArray(value)) {
    value.forEach((entry) => appendFormValue(formData, key, entry));
    return;
  }

  if (value instanceof File) {
    formData.append(key, value);
    return;
  }

  formData.append(key, String(value));
}

function toMarketplaceFormData(payload: MarketplaceFormPayload) {
  const formData = new FormData();
  Object.entries(payload).forEach(([key, value]) => appendFormValue(formData, key, value));
  return formData;
}

export async function getMarketplaceHome(params: Record<string, string | number | boolean | null | undefined> = {}) {
  return fetchWithAuth<MarketplaceHomeResponse>(apiEndpoints.marketplace.home(params));
}

export async function getMarketplaceShopByAdminId(adminId: string) {
  return fetchWithAuth<MarketplaceShopProfile>(apiEndpoints.marketplace.shopByAdminId(adminId));
}

export async function getAdminShopProfile() {
  return fetchWithAuth<MarketplaceShopProfile>(apiEndpoints.marketplace.adminMyShopProfile);
}

export async function updateAdminShopProfile(payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceShopProfile>(apiEndpoints.marketplace.adminShopProfile, {
    method: "PATCH",
    body: toMarketplaceFormData(payload),
  });
}

export async function createAdminRepairShop(payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceRepairShop>(apiEndpoints.marketplace.adminCreateRepairShop, {
    method: "POST",
    body: toMarketplaceFormData(payload),
  });
}

export async function updateAdminRepairShop(repairShopId: string, payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceRepairShop>(apiEndpoints.marketplace.adminUpdateRepairShop(repairShopId), {
    method: "PATCH",
    body: toMarketplaceFormData(payload),
  });
}

export async function deleteAdminRepairShop(repairShopId: string) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.marketplace.adminDeleteRepairShop(repairShopId), {
    method: "DELETE",
  });
}

export async function listAdminRepairShops(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<MarketplaceRepairShopResponse>(apiEndpoints.marketplace.adminMyRepairShops(params));
}

export async function createAdminInstrument(payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.adminCreateInstrument, {
    method: "POST",
    body: toMarketplaceFormData(payload),
  });
}

export async function updateAdminInstrument(instrumentId: string, payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.adminUpdateInstrument(instrumentId), {
    method: "PATCH",
    body: toMarketplaceFormData(payload),
  });
}

export async function deleteAdminInstrument(instrumentId: string) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.marketplace.adminDeleteInstrument(instrumentId), {
    method: "DELETE",
  });
}

export async function listAdminInstruments(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<MarketplaceResponse>(apiEndpoints.marketplace.adminMyInstruments(params));
}

export async function createAdminListing(payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.adminCreateListing, {
    method: "POST",
    body: toMarketplaceFormData(payload),
  });
}

export async function updateAdminListing(listingId: string, payload: MarketplaceFormPayload) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.adminUpdateListing(listingId), {
    method: "PATCH",
    body: toMarketplaceFormData(payload),
  });
}

export async function deleteAdminListing(listingId: string) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.marketplace.adminDeleteListing(listingId), {
    method: "DELETE",
  });
}

export async function listAdminListings(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<MarketplaceResponse>(apiEndpoints.marketplace.adminMyListings(params));
}

export async function listModerationListings(params: Record<string, string | number | boolean | null | undefined> = {}) {
  return fetchWithAuth<MarketplaceResponse>(apiEndpoints.marketplace.moderationListings(params));
}

export async function updateModerationListingStatus(
  listingId: string,
  isActive: boolean,
  reason?: string,
) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.moderationUpdateListingStatus(listingId), {
    method: "PATCH",
    body: JSON.stringify({ isActive, reason }),
  });
}

export async function deleteModerationListing(listingId: string) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.marketplace.moderationDeleteListing(listingId), {
    method: "DELETE",
  });
}

export async function listModerationRepairShops(
  params: Record<string, string | number | boolean | null | undefined> = {},
) {
  return fetchWithAuth<MarketplaceRepairShopResponse>(apiEndpoints.marketplace.moderationRepairShops(params));
}

export async function updateModerationRepairShopStatus(
  repairShopId: string,
  isActive: boolean,
  reason?: string,
) {
  return fetchWithAuth<MarketplaceRepairShop>(
    apiEndpoints.marketplace.moderationUpdateRepairShopStatus(repairShopId),
    {
      method: "PATCH",
      body: JSON.stringify({ isActive, reason }),
    },
  );
}

export async function deleteModerationRepairShop(repairShopId: string) {
  return fetchWithAuth<SuccessMessage>(apiEndpoints.marketplace.moderationDeleteRepairShop(repairShopId), {
    method: "DELETE",
  });
}

export async function createMarketplaceListingForSuperAdmin(
  adminId: string,
  payload: MarketplaceFormPayload,
) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.superAdminCreateListing(adminId), {
    method: "POST",
    body: toMarketplaceFormData(payload),
  });
}

export async function createMarketplaceInstrumentForSuperAdmin(
  adminId: string,
  payload: MarketplaceFormPayload,
) {
  return fetchWithAuth<MarketplaceListing>(apiEndpoints.marketplace.superAdminCreateInstrument(adminId), {
    method: "POST",
    body: toMarketplaceFormData(payload),
  });
}

export async function createMarketplaceRepairShopForSuperAdmin(
  adminId: string,
  payload: MarketplaceFormPayload,
) {
  return fetchWithAuth<MarketplaceRepairShop>(
    apiEndpoints.marketplace.superAdminCreateRepairShop(adminId),
    {
      method: "POST",
      body: toMarketplaceFormData(payload),
    },
  );
}

export async function updateMarketplaceShopProfileForSuperAdmin(
  adminId: string,
  payload: MarketplaceFormPayload,
) {
  return fetchWithAuth<MarketplaceShopProfile>(
    apiEndpoints.marketplace.superAdminUpdateShopProfile(adminId),
    {
      method: "PATCH",
      body: toMarketplaceFormData(payload),
    },
  );
}
