// frontend/src/api.js
import axios from 'axios';

const API_BASE = 'http://localhost:8000/api';

export const saveResume = async (resumeData) => {
  try {
    const response = await axios.post(`${API_BASE}/save_resume.php`, resumeData, {
      headers: { 'Content-Type': 'application/json' }
    });
    return response.data;
  } catch (error) {
    console.error('Save failed:', error);
    return null;
  }
};

export const getResume = async (id) => {
  if (!id) return null;
  try {
    const response = await axios.get(`${API_BASE}/get_resume.php?id=${id}`);
    return response.data;
  } catch (error) {
    console.error('Load failed:', error);
    return null;
  }
};

export const enhanceWithAI = async (sectionType, content) => {
  try {
    const response = await axios.post(`${API_BASE}/ai_enhance.php`, {
      section_type: sectionType,
      content: typeof content === 'string' ? content : JSON.stringify(content)
    });
    const enhanced = response.data.enhanced;
    if (sectionType === 'skills' && Array.isArray(content)) {
      return enhanced.split(',').map(s => s.trim());
    }
    return enhanced;
  } catch (error) {
    console.error('AI enhancement failed:', error);
    return content;
  }
};