import axios, { type AxiosRequestConfig } from 'axios';
import type { Radio } from '../types/radio.types';

export class APIService {
	private readonly client;

	constructor(baseURL: string = '/api/v1') {
		this.client = axios.create({
			baseURL,
			headers: {
				Accept: 'application/json',
			},
		});
	}

	public async fetch<T>(endpoint: string, config?: AxiosRequestConfig): Promise<T> {
		const response = await this.client.get<T>(endpoint, config);
		return response.data as T;
	}

	public async fetchRadios(protocol?: string): Promise<Radio[]> {
		const endpoint = protocol ? `/radios/${encodeURIComponent(protocol)}` : '/radios';
		return this.fetch<Radio[]>(endpoint);
	}

	public async fetchAllRadios(): Promise<Radio[]> {
		return this.fetch<Radio[]>('/radios');
	}
}

const API = new APIService();
export default API;
