import api from './api';

export interface AIGenerateBlogReq {
  prompt: string;
  audience?: string;
  min_words?: number;
}

export interface AIGenerateBlogResp {
  title: string;
  slug: string;
  html: string;
}

export async function generateBlogAI(payload: AIGenerateBlogReq): Promise<AIGenerateBlogResp> {
  const { data } = await api.post<AIGenerateBlogResp>('/ai/blog/generate', payload);
  
  // Handle potential JSON string response from AI (defensive parsing)
  let parsedData = data;
  if (typeof data === 'string') {
    try {
      parsedData = JSON.parse(data);
    } catch {
      throw new Error('AI vrátila neplatný formát odpovědi');
    }
  }
  
  return parsedData;
}

export interface AIGenerateAboutReq {
  prompt: string;
  club_name?: string;
  style?: string;
  audience?: string;
}

export interface AIGenerateAboutResp {
  title: string;
  subtitle: string;
  html: string;
  seo_title: string;
  seo_description: string;
}

export async function generateAboutAI(payload: AIGenerateAboutReq): Promise<AIGenerateAboutResp> {
  const { data } = await api.post<AIGenerateAboutResp>('/ai/about/generate', payload);
  
  // Handle potential JSON string response from AI (defensive parsing)
  let parsedData = data;
  if (typeof data === 'string') {
    try {
      parsedData = JSON.parse(data);
    } catch {
      throw new Error('AI vrátila neplatný formát odpovědi');
    }
  }
  
  return parsedData;
}
