import api from './api'; import { getMediaUrl } from '../media'; export interface Video { id: string; title: string; doctor: string; duration: string; thumbnail: string; videoUrl: string; description: string; viewsCount?: number; isFeatured?: boolean; } function normalizeVideo(v: any): Video { return { id: String(v.id || ''), title: String(v.title || ''), doctor: String(v.doctor || 'کادر علمی کنینا'), duration: String(v.duration || '۳:۰۰'), thumbnail: getMediaUrl(v.thumbnail || v.coverUrl || '/assets/images/hero-section-image.png'), videoUrl: getMediaUrl(v.videoUrl || ''), description: String(v.description || ''), viewsCount: Number(v.viewsCount) || 0, isFeatured: Boolean(v.isFeatured), }; } export const videoService = { getVideos: async (params?: { featured?: boolean; limit?: number; search?: string }): Promise => { try { const res = await api.get('/videos', { params }); const raw = res.data; let items: any[] = []; if (Array.isArray(raw?.videos)) items = raw.videos; else if (Array.isArray(raw?.data?.videos)) items = raw.data.videos; else if (Array.isArray(raw?.data)) items = raw.data; else if (Array.isArray(raw)) items = raw; return items.map(normalizeVideo); } catch (error) { console.error('Failed to fetch videos from API:', error); return []; } }, getVideoById: async (id: string) => { try { const res = await api.get(`/videos/${id}`); return res.data; } catch (error) { console.error('Failed to fetch video details:', error); return null; } }, incrementViews: async (id: string) => { try { await api.patch(`/videos/${id}/view`); } catch (error) { console.warn('Failed to increment video views:', error); } } };