import { api } from "@/lib/api-client";

export type CefrLevel = "A1" | "A2" | "B1" | "B2" | "C1" | "C2";

export interface VocabItem {
  id: number;
  word: string;
  meaning: string;
  example_sentence: string;
  level: CefrLevel;
  part_of_speech: "noun" | "verb" | "adjective" | "adverb" | "phrase" | "other";
  category: string;
  active: boolean;
  learners_count: number;
  completion_count: number;
  completed: boolean;
  mastery: "learning" | "familiar" | "mastered";
  last_reviewed: string;
}

export interface GrammarRule {
  id: number;
  name: string;
  description: string;
  example: string;
  level: CefrLevel;
  category: string;
  active: boolean;
  learners_count: number;
  completion_count: number;
  completed: boolean;
  last_reviewed: string;
}

export interface TrainingSummary {
  total: number;
  completed: number;
  remaining: number;
  completion_rate: number;
}

export interface VocabListResponse {
  items: VocabItem[];
  total: number;
  summary: TrainingSummary;
}

export interface GrammarListResponse {
  items: GrammarRule[];
  total: number;
  summary: TrainingSummary;
}

export interface VocabFilters {
  level?: string;
  category?: string;
  search?: string;
  active?: string;
}

export interface GrammarFilters {
  level?: string;
  category?: string;
  search?: string;
  active?: string;
}

export type VocabCreatePayload = Omit<
  VocabItem,
  "id" | "completed" | "mastery" | "last_reviewed" | "learners_count" | "completion_count"
>;

export type GrammarCreatePayload = Omit<
  GrammarRule,
  "id" | "completed" | "last_reviewed" | "learners_count" | "completion_count"
>;

export const trainingService = {
  // Vocabulary
  listVocab: (filters?: VocabFilters) =>
    api.get<VocabListResponse>(
      "/training/vocabulary",
      filters as Record<string, string | number | boolean | undefined>,
    ),
  createVocab: (payload: Partial<VocabCreatePayload>) =>
    api.post<VocabItem>("/training/vocabulary", payload),
  updateVocab: (id: number, payload: Partial<VocabItem>) =>
    api.patch<VocabItem>(`/training/vocabulary/${id}`, payload),
  deleteVocab: (id: number) =>
    api.delete<{ success: boolean }>(`/training/vocabulary/${id}`),
  setVocabProgress: (
    id: number,
    payload: { completed?: boolean; mastery?: VocabItem["mastery"] },
  ) => api.post<VocabItem>(`/training/vocabulary/${id}/progress`, payload),

  // Grammar
  listGrammar: (filters?: GrammarFilters) =>
    api.get<GrammarListResponse>(
      "/training/grammar",
      filters as Record<string, string | number | boolean | undefined>,
    ),
  createGrammar: (payload: Partial<GrammarCreatePayload>) =>
    api.post<GrammarRule>("/training/grammar", payload),
  updateGrammar: (id: number, payload: Partial<GrammarRule>) =>
    api.patch<GrammarRule>(`/training/grammar/${id}`, payload),
  deleteGrammar: (id: number) =>
    api.delete<{ success: boolean }>(`/training/grammar/${id}`),
  setGrammarProgress: (id: number, payload: { completed?: boolean }) =>
    api.post<GrammarRule>(`/training/grammar/${id}/progress`, payload),
};
