import { apiRequest, apiUpload, type HelpAttachmentPayload } from "@/services/apiClient";
import type { ApiPaginatedResponse, ApiResponse } from "@/types";
import type { HelpArticle, HelpArticleDetail, HelpCategory } from "@/types/help";

export const helpService = {
  async categories() {
    return apiRequest<HelpCategory[]>("/help/categories");
  },

  async articles(params?: {
    category?: string;
    featured?: boolean;
    search?: string;
    page?: number;
    perPage?: number;
  }) {
    const qs = new URLSearchParams();
    if (params?.category) qs.set("category", params.category);
    if (params?.featured) qs.set("featured", "1");
    if (params?.search) qs.set("search", params.search);
    if (params?.page) qs.set("page", String(params.page));
    if (params?.perPage) qs.set("per_page", String(params.perPage));
    const query = qs.toString();
    return apiRequest<HelpArticle[]>(`/help/articles${query ? `?${query}` : ""}`) as Promise<
      ApiPaginatedResponse<HelpArticle> | ApiResponse<HelpArticle[]>
    >;
  },

  async article(slug: string) {
    return apiRequest<HelpArticleDetail>(`/help/articles/${slug}`);
  },

  async suggest(q: string) {
    return apiRequest<HelpArticle[]>(`/help/suggest?q=${encodeURIComponent(q)}`);
  },

  async feedback(slug: string, helpful: boolean, sessionId?: string) {
    return apiRequest<null>(`/help/articles/${slug}/feedback`, {
      method: "POST",
      body: { helpful, session_id: sessionId },
    });
  },

  async uploadAttachment(file: File) {
    const form = new FormData();
    form.append("file", file);
    return apiUpload<HelpAttachmentPayload>("/help/attachments", form);
  },

  async createTicket(body: {
    subject: string;
    message?: string;
    attachments?: HelpAttachmentPayload[];
    name?: string;
    email?: string;
    phone: string;
    is_customer: boolean;
    ticket_category: string;
    preferred_contact: string;
    company?: string;
    project_reference?: string;
    priority?: string;
    source?: string;
  }) {
    return apiRequest<{ id: number; botReply?: import("@/types/helpChat").HelpBotReply; botActive?: boolean }>(
      "/help/tickets",
      { method: "POST", body },
    );
  },

  async appendTicketMessage(
    ticketId: number,
    body: { message?: string; attachments?: HelpAttachmentPayload[] },
  ) {
    return apiRequest<{
      id: number;
      botReply?: import("@/types/helpChat").HelpBotReply;
      botActive?: boolean;
    }>(`/help/tickets/${ticketId}/messages`, {
      method: "POST",
      body,
    });
  },

  async ticketMessages(ticketId: number) {
    return apiRequest<
      {
        id: number;
        senderType: string;
        message: string;
        attachments?: HelpAttachmentPayload[];
        author?: { id: number; name: string };
        createdAt?: string;
      }[]
    >(`/help/tickets/${ticketId}/messages`) as Promise<
      import("@/types").ApiResponse<
        {
          id: number;
          senderType: string;
          message: string;
          attachments?: HelpAttachmentPayload[];
          author?: { id: number; name: string };
          createdAt?: string;
        }[]
      > & { meta?: { botActive?: boolean; assignedTo?: number | null } }
    >;
  },
};
