import { api } from "@/lib/api-client";
import type { Subject, Domain, Topic, LearningObjective, TaxonomyTree, PaginatedResponse, PaginationParams, ApiSuccessResponse } from "@/types";

export const taxonomyService = {
  async listSubjects(): Promise<Subject[]> {
    const res = await api.get<Record<string, unknown>>("/subjects");
    if (Array.isArray(res)) return res;
    const r = res as { data?: Subject[]; items?: Subject[]; subjects?: Subject[] };
    return r.data ?? r.items ?? r.subjects ?? [];
  },

  async getSubject(id: number): Promise<Subject> {
    const res = await api.get<{ data: Subject } | Subject>(`/subjects/${id}`);
    const wrapped = res as { data?: Subject };
    return (wrapped.data ?? (res as Subject));
  },

  async createSubject(data: Partial<Subject>): Promise<Subject> {
    return api.post<Subject>("/subjects", data);
  },

  async updateSubject(id: number, data: Partial<Subject>): Promise<Subject> {
    return api.patch<Subject>(`/subjects/${id}`, data);
  },

  async deleteSubject(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/subjects/${id}`);
  },

  async getTaxonomyTree(subjectId: number): Promise<TaxonomyTree> {
    const res = await api.get<{ data: TaxonomyTree } | TaxonomyTree>(`/subjects/${subjectId}/taxonomy`);
    const wrapped = res as { data?: TaxonomyTree };
    return (wrapped.data ?? (res as TaxonomyTree));
  },

  async importTaxonomy(subjectId: number, data: FormData): Promise<ApiSuccessResponse> {
    return api.upload<ApiSuccessResponse>(`/subjects/${subjectId}/taxonomy/import`, data);
  },

  async listDomains(params?: { subject_id?: number }): Promise<Domain[]> {
    const res = await api.get<Record<string, unknown>>("/domains", params as Record<string, string | number | boolean | undefined>);
    if (Array.isArray(res)) return res;
    const r = res as { data?: Domain[]; items?: Domain[] };
    return r.data ?? r.items ?? [];
  },

  async createDomain(data: Partial<Domain>): Promise<Domain> {
    return api.post<Domain>("/domains", data);
  },

  async updateDomain(id: number, data: Partial<Domain>): Promise<Domain> {
    return api.patch<Domain>(`/domains/${id}`, data);
  },

  async deleteDomain(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/domains/${id}`);
  },

  async aiSuggestTopics(domainId: number): Promise<{ suggestions: Partial<Topic>[] }> {
    return api.post(`/domains/${domainId}/ai-suggest`);
  },

  async listTopics(params?: { domain_id?: number; subject_id?: number }): Promise<Topic[]> {
    const res = await api.get<Record<string, unknown>>("/topics", params as Record<string, string | number | boolean | undefined>);
    if (Array.isArray(res)) return res;
    const r = res as { data?: Topic[]; items?: Topic[] };
    return r.data ?? r.items ?? [];
  },

  async createTopic(data: Partial<Topic>): Promise<Topic> {
    return api.post<Topic>("/topics", data);
  },

  async updateTopic(id: number, data: Partial<Topic>): Promise<Topic> {
    return api.patch<Topic>(`/topics/${id}`, data);
  },

  async deleteTopic(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/topics/${id}`);
  },
};
