import api from './api';
import type { FeedbackAction, HistoryEntry } from '../types';

export const historyService = {
  // POST /api/history { trackId, completed?, batchId?, listenedMs? } -> { historyId }
  async recordPlay(
    trackId: string,
    completed?: boolean,
    batchId?: string,
    listenedMs?: number
  ): Promise<{ historyId: string }> {
    const res = await api.post<{ historyId: string }>('/history', {
      trackId,
      completed,
      batchId,
      listenedMs,
    });
    return res.data;
  },

  // POST /api/history/skip { trackId } -> { status }
  async skip(trackId: string): Promise<{ status: string }> {
    const res = await api.post<{ status: string }>('/history/skip', { trackId });
    return res.data;
  },

  // GET /api/history -> recent play history (Track + playback metadata)
  async list(): Promise<HistoryEntry[]> {
    const res = await api.get<HistoryEntry[]>('/history');
    return res.data;
  },

  // POST /api/feedback { trackId, action } -> { status }
  async feedback(trackId: string, action: FeedbackAction): Promise<{ status: string }> {
    const res = await api.post<{ status: string }>('/feedback', { trackId, action });
    return res.data;
  },
};
