import { apiClient } from '@/services/api/client';
import { requireFeature } from '@/config/features';

/**
 * Service HLS pour gérer les URLs et informations de streaming HLS
 * T0335: Create HLS Streaming Frontend Service
 *
 * ⚠️ MVP: This feature is disabled. Backend endpoints are not implemented.
 * TODO: Enable when backend implements /api/v1/tracks/:id/hls/* endpoints
 *
 * @see FEATURES.HLS_STREAMING
 */

/**
 * Génère l'URL du master playlist pour un track
 * @param trackId - ID du track
 * @returns URL du master playlist
 */
export function getHLSMasterPlaylistURL(trackId: string): string {
  const baseURL = apiClient.defaults.baseURL || '';
  // Enlever le /api/v1 de la fin si présent, car on va ajouter /api/tracks
  const cleanBaseURL = baseURL.replace(/\/api\/v1$/, '');
  return `${cleanBaseURL}/api/v1/tracks/${trackId}/hls/master.m3u8`;
}

/**
 * Génère l'URL d'une quality playlist pour un track et bitrate
 * @param trackId - ID du track
 * @param bitrate - Bitrate (ex: "128k", "192k", "320k")
 * @returns URL de la quality playlist
 */
export function getHLSQualityPlaylistURL(
  trackId: string,
  bitrate: string,
): string {
  const baseURL = apiClient.defaults.baseURL || '';
  const cleanBaseURL = baseURL.replace(/\/api\/v1$/, '');
  return `${cleanBaseURL}/api/v1/tracks/${trackId}/hls/${bitrate}/playlist.m3u8`;
}

/**
 * Génère l'URL d'un segment pour un track, bitrate et nom de segment
 * @param trackId - ID du track
 * @param bitrate - Bitrate (ex: "128k", "192k", "320k")
 * @param segment - Nom du segment (ex: "segment_000.ts")
 * @returns URL du segment
 */
export function getHLSSegmentURL(
  trackId: string,
  bitrate: string,
  segment: string,
): string {
  const baseURL = apiClient.defaults.baseURL || '';
  const cleanBaseURL = baseURL.replace(/\/api\/v1$/, '');
  return `${cleanBaseURL}/api/v1/tracks/${trackId}/hls/${bitrate}/${segment}`;
}

/**
 * Interface pour les informations d'un stream HLS
 */
export interface HLSStreamInfo {
  trackId: string;
  bitrates: number[];
  playlistUrl: string;
}

/**
 * Interface pour le statut d'un stream HLS (T0339, T0340)
 */
export interface HLSStreamStatus {
  status: 'pending' | 'processing' | 'ready' | 'failed';
  bitrates: number[];
  segments_count: number;
  playlist_url: string;
  track_id: string;
  created_at?: string;
  updated_at?: string;
  queue_job_id?: string;
  retry_count?: number;
  started_at?: string;
}

/**
 * Récupère les informations d'un stream HLS pour un track
 * @param trackId - ID du track
 * @returns Informations du stream HLS
 */
export async function getHLSStreamInfo(
  trackId: string,
): Promise<HLSStreamInfo> {
  requireFeature('HLS_STREAMING');
  const response = await apiClient.get(`/tracks/${trackId}/hls/info`);
  return response.data;
}

/**
 * Récupère le statut d'un stream HLS pour un track (T0339, T0340)
 * @param trackId - ID du track
 * @returns Statut du stream HLS
 */
export async function getHLSStreamStatus(
  trackId: string,
): Promise<HLSStreamStatus> {
  requireFeature('HLS_STREAMING');
  const response = await apiClient.get(`/tracks/${trackId}/hls/status`);
  return response.data;
}
