import api from './api';
import type { InfluencerProfile, MediaPost } from '../types/influencer';

export const influencerService = {
  async getProfile(): Promise<InfluencerProfile> {
    const response = await api.get<InfluencerProfile>('/api/influencers/profiles/me/');
    return response.data;
  },

  async getInstagramConnectUrl(): Promise<string> {
    const response = await api.get<{ oauth_url: string }>('/api/influencers/instagram/connect-url/');
    return response.data.oauth_url;
  },

  async refreshData(profileId: number): Promise<void> {
    await api.post(`/api/influencers/profiles/${profileId}/refresh-data/`);
  },

  async getMediaPosts(profileId?: number): Promise<MediaPost[]> {
    const params = profileId ? { profile_id: profileId } : {};
    const response = await api.get('/api/influencers/media-posts/', { params });
    const data = response.data;
    if (Array.isArray(data)) return data as MediaPost[];
    if (data && typeof data === 'object' && 'results' in data) return (data as { results: MediaPost[] }).results;
    return [];
  },

  async getNiches(): Promise<string[]> {
    const response = await api.get<{ niches: string[] }>('/api/influencers/niches/');
    return response.data.niches;
  },
};
