import http from '@/http';

export interface AnnonceType {
  id: number;
  titre: string;
  soustitre: string;
}

export interface AnnonceAuthor {
  id: number;
  pseudo: string;
}

export interface Annonce {
  id: number;
  titre: string;
  texte: string | null;
  date: string | null;
  typeId: number;
  auteur: AnnonceAuthor | null;
}

export async function getAnnonceTypes(): Promise<AnnonceType[]> {
  const response = await http.get('/annonce-types');
  return response.data;
}

export async function getAnnonces(typeId?: number): Promise<Annonce[]> {
  const url = typeId ? `/annonces?typeId=${typeId}` : '/annonces';
  const response = await http.get(url);
  return response.data;
}

export async function createAnnonce(data: {
  titre: string;
  texte: string;
  typeId: number;
}): Promise<Annonce> {
  const response = await http.post('/annonces', data);
  return response.data;
}

export async function deleteAnnonce(id: number): Promise<void> {
  await http.delete(`/annonces/${id}`);
}

/** Modification par le propriétaire : seuls les champs fournis sont envoyés. */
export async function updateAnnonce(id: number, data: {titre?: string; texte?: string; typeId?: number}): Promise<Annonce> {
  const response = await http.patch(`/annonces/${id}`, data);
  return response.data;
}

/** Réponse à une annonce : l'API transmet le message à l'annonceur par email. */
export async function repondreAnnonce(id: number, message: string): Promise<void> {
  await http.post(`/annonces/${id}/reponses`, {message});
}
