import { apiRequest } from "@/services/apiClient";
import type {
  AppCreationCategory,
  AppCreationOptional,
  AppCreationPlan,
} from "@/types/appCreation";

export const adminAppCreationService = {
  plans: {
    list: (token: string, params?: { search?: string; active?: boolean }) => {
      const q = new URLSearchParams();
      if (params?.search) q.set("search", params.search);
      if (params?.active !== undefined) q.set("active", String(params.active));
      const qs = q.toString();
      return apiRequest<AppCreationPlan[]>(
        `/admin/app-creation/plans${qs ? `?${qs}` : ""}`,
        { token },
      );
    },
    get: (token: string, id: number) =>
      apiRequest<AppCreationPlan>(`/admin/app-creation/plans/${id}`, { token }),
    create: (token: string, body: Record<string, unknown>) =>
      apiRequest<AppCreationPlan>("/admin/app-creation/plans", {
        method: "POST",
        token,
        body,
      }),
    update: (token: string, id: number, body: Record<string, unknown>) =>
      apiRequest<AppCreationPlan>(`/admin/app-creation/plans/${id}`, {
        method: "PUT",
        token,
        body,
      }),
    toggle: (token: string, id: number) =>
      apiRequest<AppCreationPlan>(`/admin/app-creation/plans/${id}/toggle`, {
        method: "POST",
        token,
      }),
    remove: (token: string, id: number) =>
      apiRequest<null>(`/admin/app-creation/plans/${id}`, {
        method: "DELETE",
        token,
      }),
  },
  categories: {
    list: (token: string) =>
      apiRequest<AppCreationCategory[]>("/admin/app-creation/categories", { token }),
    create: (token: string, body: Record<string, unknown>) =>
      apiRequest<AppCreationCategory>("/admin/app-creation/categories", {
        method: "POST",
        token,
        body,
      }),
    update: (token: string, id: number, body: Record<string, unknown>) =>
      apiRequest<AppCreationCategory>(`/admin/app-creation/categories/${id}`, {
        method: "PUT",
        token,
        body,
      }),
    toggle: (token: string, id: number) =>
      apiRequest<AppCreationCategory>(
        `/admin/app-creation/categories/${id}/toggle`,
        { method: "POST", token },
      ),
    remove: (token: string, id: number) =>
      apiRequest<null>(`/admin/app-creation/categories/${id}`, {
        method: "DELETE",
        token,
      }),
  },
  optionals: {
    list: (token: string, params?: { search?: string; category_id?: number }) => {
      const q = new URLSearchParams();
      if (params?.search) q.set("search", params.search);
      if (params?.category_id) q.set("category_id", String(params.category_id));
      const qs = q.toString();
      return apiRequest<AppCreationOptional[]>(
        `/admin/app-creation/optionals${qs ? `?${qs}` : ""}`,
        { token },
      );
    },
    create: (token: string, body: Record<string, unknown>) =>
      apiRequest<AppCreationOptional>("/admin/app-creation/optionals", {
        method: "POST",
        token,
        body,
      }),
    update: (token: string, id: number, body: Record<string, unknown>) =>
      apiRequest<AppCreationOptional>(`/admin/app-creation/optionals/${id}`, {
        method: "PUT",
        token,
        body,
      }),
    toggle: (token: string, id: number) =>
      apiRequest<AppCreationOptional>(
        `/admin/app-creation/optionals/${id}/toggle`,
        { method: "POST", token },
      ),
    remove: (token: string, id: number) =>
      apiRequest<null>(`/admin/app-creation/optionals/${id}`, {
        method: "DELETE",
        token,
      }),
  },
  contracts: {
    list: (token: string) =>
      apiRequest<
        {
          id: number;
          user: { id: number; name: string; email: string } | null;
          plan: { id: number; name: string; slug: string } | null;
          status: string;
          quote: { grandTotal: number; status: string } | null;
        }[]
      >("/admin/app-creation/contracts", { token }),
  },
};
