import api from './api';

// Types
export interface ContactCategory {
  id: number;
  name: string;
  description: string;
  display_order: number;
  is_active: boolean;
  created_at?: string;
  updated_at?: string;
}

export interface Contact {
  id: number;
  category_id?: number;
  category?: ContactCategory;
  name: string;
  position: string;
  email: string;
  phone: string;
  image_url?: string;
  description?: string;
  display_order: number;
  is_active: boolean;
  created_at?: string;
  updated_at?: string;
}

export interface GroupedContacts {
  categories: Record<string, Contact[]>;
  uncategorized: Contact[];
}

// Public API
export const getPublicContacts = async (): Promise<GroupedContacts> => {
  const response = await api.get('/contacts');
  return response.data;
};

export const getPublicContactCategories = async (): Promise<ContactCategory[]> => {
  const response = await api.get('/contacts/categories');
  return response.data;
};

// Admin API - Contact Categories
export const getContactCategories = async (): Promise<ContactCategory[]> => {
  const response = await api.get('/admin/contacts/categories');
  return response.data;
};

export const createContactCategory = async (data: Partial<ContactCategory>): Promise<ContactCategory> => {
  const response = await api.post('/admin/contacts/categories', data);
  return response.data;
};

export const updateContactCategory = async (id: number, data: Partial<ContactCategory>): Promise<ContactCategory> => {
  const response = await api.put(`/admin/contacts/categories/${id}`, data);
  return response.data;
};

export const deleteContactCategory = async (id: number): Promise<void> => {
  await api.delete(`/admin/contacts/categories/${id}`);
};

// Admin API - Contacts
export const getContacts = async (): Promise<Contact[]> => {
  const response = await api.get('/admin/contacts');
  return response.data;
};

export const createContact = async (data: Partial<Contact>): Promise<Contact> => {
  const response = await api.post('/admin/contacts', data);
  return response.data;
};

export const updateContact = async (id: number, data: Partial<Contact>): Promise<Contact> => {
  const response = await api.put(`/admin/contacts/${id}`, data);
  return response.data;
};

export const deleteContact = async (id: number): Promise<void> => {
  await api.delete(`/admin/contacts/${id}`);
};
