import axios from 'axios';

const API_BASE_URL = process.env.REACT_APP_API_URL || 'http://localhost:8080/api/v1';

export interface NavigationItem {
  id?: number;
  label: string;
  url?: string;
  icon?: string;
  type: 'internal' | 'external' | 'dropdown' | 'page';
  page_type?: string;
  page_id?: number;
  visible: boolean;
  display_order: number;
  parent_id?: number;
  children?: NavigationItem[];
  target?: '_self' | '_blank';
  css_class?: string;
  requires_auth?: boolean;
  requires_admin?: boolean;
}

export interface SocialLink {
  id?: number;
  platform: string;
  url: string;
  display_order: number;
  visible: boolean;
  icon?: string;
}

// Public endpoints
export const getNavigationItems = async (): Promise<NavigationItem[]> => {
  const response = await axios.get(`${API_BASE_URL}/navigation`);
  return response.data;
};

export const getSocialLinks = async (): Promise<SocialLink[]> => {
  const response = await axios.get(`${API_BASE_URL}/social-links`);
  return response.data;
};

// Admin endpoints
export const getAllNavigationItems = async (): Promise<NavigationItem[]> => {
  const response = await axios.get(`${API_BASE_URL}/admin/navigation`, {
    withCredentials: true,
  });
  return response.data;
};

export const createNavigationItem = async (item: Partial<NavigationItem>): Promise<NavigationItem> => {
  const response = await axios.post(`${API_BASE_URL}/admin/navigation`, item, {
    withCredentials: true,
  });
  return response.data;
};

export const updateNavigationItem = async (id: number, item: Partial<NavigationItem>): Promise<NavigationItem> => {
  const response = await axios.put(`${API_BASE_URL}/admin/navigation/${id}`, item, {
    withCredentials: true,
  });
  return response.data;
};

export const deleteNavigationItem = async (id: number): Promise<void> => {
  await axios.delete(`${API_BASE_URL}/admin/navigation/${id}`, {
    withCredentials: true,
  });
};

export const reorderNavigationItems = async (orders: { id: number; display_order: number }[]): Promise<void> => {
  await axios.post(`${API_BASE_URL}/admin/navigation/reorder`, orders, {
    withCredentials: true,
  });
};

// Social links admin endpoints
export const getAllSocialLinks = async (): Promise<SocialLink[]> => {
  const response = await axios.get(`${API_BASE_URL}/admin/social-links`, {
    withCredentials: true,
  });
  return response.data;
};

export const createSocialLink = async (link: Partial<SocialLink>): Promise<SocialLink> => {
  const response = await axios.post(`${API_BASE_URL}/admin/social-links`, link, {
    withCredentials: true,
  });
  return response.data;
};

export const updateSocialLink = async (id: number, link: Partial<SocialLink>): Promise<SocialLink> => {
  const response = await axios.put(`${API_BASE_URL}/admin/social-links/${id}`, link, {
    withCredentials: true,
  });
  return response.data;
};

export const deleteSocialLink = async (id: number): Promise<void> => {
  await axios.delete(`${API_BASE_URL}/admin/social-links/${id}`, {
    withCredentials: true,
  });
};

export const reorderSocialLinks = async (orders: { id: number; display_order: number }[]): Promise<void> => {
  await axios.post(`${API_BASE_URL}/admin/social-links/reorder`, orders, {
    withCredentials: true,
  });
};

export const seedDefaultNavigation = async (): Promise<{ message: string; count: number; seeded: boolean }> => {
  const response = await axios.post(`${API_BASE_URL}/admin/navigation/seed`, {}, {
    withCredentials: true,
  });
  return response.data;
};
