import { api } from './client';
import type {
  User, Character, Monster, CombatLog,
  CharacterItem, CharacterMaterial, Recipe, CraftJob, Item,
} from './types';

// Auth
export const authApi = {
  setSession: (token: string, refreshToken?: string) =>
    api.post<User>('/auth/session', { token, refreshToken }),
  me:         ()               => api.get<User>('/auth/me'),
  logout:     ()               => api.post<void>('/auth/logout'),
};

// Character
export const characterApi = {
  create: (name: string, stats: Record<string, number>) =>
    api.post<Character>('/characters', { name, ...stats }),
  me:     () => api.get<Character>('/characters/me'),
  distributeStats: (stats: Record<string, number>) =>
    api.post<Character>('/characters/stats', stats),
  rest:   () => api.post<{ hpBefore: number; hpAfter: number; hpMax: number; healed: number }>('/characters/rest'),
  setTitle: (title: string | null) => api.put<any>('/profile/title', { title }),
};

// Combat
export const combatApi = {
  zones:     ()                                         => api.get<any[]>('/monsters/zones'),
  monsters:  ()                                         => api.get<Monster[]>('/monsters'),
  start:     (monsterId: string, attackType: string, count?: number) => api.post<any>('/combat/start', { monsterId, attackType, ...(count && count > 1 ? { count } : {}) }),
  history:   ()                                         => api.get<CombatLog[]>('/combat/history'),
};

// Items
export const itemApi = {
  catalogue: ()           => api.get<Item[]>('/items'),
  inventory: ()           => api.get<CharacterItem[]>('/items/inventory'),
  equip:     (id: string) => api.post<void>(`/items/equip/${id}`),
  unequip:   (slot: 'weapon' | 'armor') => api.post<void>(`/items/unequip/${slot}`),
};

// Materials
export const materialApi = {
  inventory: () => api.get<CharacterMaterial[]>('/materials/inventory'),
};

// Craft
export const craftApi = {
  recipes:  ()            => api.get<Recipe[]>('/craft/recipes'),
  start:    (recipeId: string) => api.post<CraftJob>('/craft/start', { recipeId }),
  active:   ()            => api.get<CraftJob | { status: 'none' }>('/craft/active'),
  collect:  (jobId: string)    => api.post<CharacterItem>(`/craft/collect/${jobId}`),
};

// Quests
export const questApi = {
  available: () => api.get<any[]>('/quests/available'),
  active:    () => api.get<any[]>('/quests/active'),
  completed: () => api.get<any[]>('/quests/completed'),
  accept:    (questId: string) => api.post<any>(`/quests/accept/${questId}`),
  claim:     (playerQuestId: string) => api.post<any>(`/quests/claim/${playerQuestId}`),
  abandon:   (playerQuestId: string) => api.post<any>(`/quests/abandon/${playerQuestId}`),
  arcs:      () => api.get<any[]>('/quests/arcs'),
};

// Forge
export const forgeApi = {
  upgrade: (charItemId: string) =>
    api.post<{ success: boolean; forgeLevel: number; item: string; goldSpent: number; message: string }>(`/forge/upgrade/${charItemId}`),
};
