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

export interface RegistrationCode {
  id: number;
  code: string;
  code_type: "individual" | "corporate";
  user_type: "student" | "teacher" | "corporate";
  max_uses: number;
  uses: number;
  used: boolean;
  expiry_date: string;
  created: string;
  entity_id: number | null;
}

export interface GeneratePayload {
  count?: number;
  code_type?: "individual" | "corporate";
  user_type?: "student" | "teacher" | "corporate";
  max_uses?: number;
  expiry_date?: string;
}

export interface PlatformPackage {
  id: number;
  name: string;
  price: number;
  duration: string;
  discount: number;
}

export interface GradingConfig {
  min_score: number;
  max_score: number;
  increment: number;
}

export const platformSettingsService = {
  listCodes: () =>
    api.get<{ items: RegistrationCode[]; total: number }>("/codes"),
  generateCodes: (payload: GeneratePayload) =>
    api.post<{ data: RegistrationCode[]; count: number }>(
      "/codes/generate",
      payload,
    ),
  deleteCode: (id: number) => api.delete<{ success: boolean }>(`/codes/${id}`),

  listPackages: () =>
    api.get<{ items: PlatformPackage[]; total: number }>("/packages"),
  createPackage: (p: Omit<PlatformPackage, "id">) =>
    api.post<PlatformPackage>("/packages", p),
  updatePackage: (id: number, p: Partial<PlatformPackage>) =>
    api.patch<PlatformPackage>(`/packages/${id}`, p),
  deletePackage: (id: number) =>
    api.delete<{ success: boolean }>(`/packages/${id}`),

  getGrading: () => api.get<GradingConfig>("/grading-config"),
  setGrading: (cfg: GradingConfig) => api.patch<GradingConfig>("/grading-config", cfg),
};
