import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; export interface SearchHistoryItem { id: string; query: string; filters_json?: string | null; created_at: string; } export interface WatchHistoryItem { id: string; provider: string; video_id: string; title?: string | null; watched_at: string; progress_seconds: number; duration_seconds: number; last_position_seconds: number; last_watched_at?: string; thumbnail?: string | null; } @Injectable({ providedIn: 'root' }) export class HistoryService { constructor(private http: HttpClient) {} // --- Search --- recordSearch(query: string, filters?: Record): Observable<{ id: string; created_at: string }> { return this.http.post<{ id: string; created_at: string }>( '/proxy/api/user/history/search', { query, filters }, { withCredentials: true } ); } getSearchHistory(limit = 50, before?: string): Observable { const params = new URLSearchParams(); params.set('limit', String(limit)); if (before) params.set('before', before); return this.http.get(`/proxy/api/user/history/search?${params.toString()}`, { withCredentials: true }); } // --- Watch --- recordWatchStart(provider: string, videoId: string, title?: string | null, watchedAt?: string, thumbnail?: string | null): Observable { return this.http.post( '/proxy/api/user/history/watch', { provider, videoId, title: title ?? null, watchedAt, thumbnail: thumbnail ?? null }, { withCredentials: true } ); } updateWatchProgress(id: string, progressSeconds: number, lastPositionSeconds?: number): Observable { return this.http.patch( `/proxy/api/user/history/watch/${encodeURIComponent(id)}`, { progressSeconds, lastPositionSeconds }, { withCredentials: true } ); } getWatchHistory(limit = 50, before?: string): Observable { const params = new URLSearchParams(); params.set('limit', String(limit)); if (before) params.set('before', before); return this.http.get(`/proxy/api/user/history/watch?${params.toString()}`, { withCredentials: true }); } // --- Delete single items --- deleteSearchItem(id: string): Observable { return this.http.delete(`/proxy/api/user/history/search/${encodeURIComponent(id)}`, { withCredentials: true }); } deleteWatchItem(id: string): Observable { return this.http.delete(`/proxy/api/user/history/watch/${encodeURIComponent(id)}`, { withCredentials: true }); } // Recherche dans l'historique des recherches searchInSearchHistory(query: string, limit = 100): Observable { const params = new URLSearchParams(); params.set('q', query); params.set('limit', String(limit)); return this.http.get( `/proxy/api/user/history/search?${params.toString()}`, { withCredentials: true } ); } // Recherche dans l'historique de visionnage searchInWatchHistory(query: string, limit = 100): Observable { const params = new URLSearchParams(); params.set('q', query); params.set('limit', String(limit)); return this.http.get( `/proxy/api/user/history/watch?${params.toString()}`, { withCredentials: true } ); } }