import axios from 'axios';
import { getApiUrl } from '@/config/api.config';

// Créer une instance axios avec la configuration de base
const api = axios.create({
  baseURL: getApiUrl().replace('/api', ''), // Supprimer /api de l'URL
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
});

// Interface pour les types de données
export interface Aeroport {
  id?: number;
  nom: string;
  code_iata?: string;
  code_oaci?: string;
  ville: string;
  region?: string;
  latitude?: number;
  longitude?: number;
  altitude?: number;
  description?: string;
  statut?: 'actif' | 'inactif' | 'en_construction';
  type?: 'international' | 'regional' | 'local';
  image_url?: string;
  cartes?: Carte[];
  documents?: Document[];
  informations?: Information[];
}

export interface Carte {
  id?: number;
  aeroport_id: number;
  titre: string;
  type_carte?: 'aerienne' | 'topographique' | 'infrastructure' | 'plan' | 'autre';
  url_image: string;
  description?: string;
  echelle?: string;
  annee?: number;
}

export interface Document {
  id?: number;
  aeroport_id: number;
  titre: string;
  type_document?: 'pdf' | 'word' | 'excel' | 'image' | 'video' | 'audio' | 'autre';
  categorie?: 'technique' | 'administratif' | 'historique' | 'statistique' | 'autre';
  url_fichier: string;
  taille_fichier?: number;
  description?: string;
  auteur?: string;
  date_publication?: Date;
}

export interface Information {
  id?: number;
  aeroport_id: number;
  categorie: string;
  titre: string;
  contenu: string;
  date_info?: Date;
  source?: string;
  est_public?: boolean;
}

// Services pour les aéroports
export const aeroportService = {
  // Récupérer tous les aéroports
  getAll: async (): Promise<Aeroport[]> => {
    const response = await api.get('/aeroports');
    return response.data;
  },

  // Récupérer un aéroport par ID
  getById: async (id: number): Promise<Aeroport> => {
    const response = await api.get(`/aeroports/${id}`);
    return response.data;
  },

  // Créer un nouvel aéroport
  create: async (aeroport: Aeroport): Promise<Aeroport> => {
    const response = await api.post('/aeroports', aeroport);
    return response.data;
  },

  // Mettre à jour un aéroport
  update: async (id: number, aeroport: Partial<Aeroport>): Promise<Aeroport> => {
    const response = await api.put(`/aeroports/${id}`, aeroport);
    return response.data;
  },

  // Supprimer un aéroport
  delete: async (id: number): Promise<void> => {
    await api.delete(`/aeroports/${id}`);
  },

  // Rechercher des aéroports
  search: async (query: string): Promise<Aeroport[]> => {
    const response = await api.get(`/aeroports/search?query=${query}`);
    return response.data;
  },
};

// Services pour les cartes
export const carteService = {
  getAll: async (): Promise<Carte[]> => {
    const response = await api.get('/cartes');
    return response.data;
  },

  getById: async (id: number): Promise<Carte> => {
    const response = await api.get(`/cartes/${id}`);
    return response.data;
  },

  getByAeroport: async (aeroportId: number): Promise<Carte[]> => {
    const response = await api.get(`/cartes/aeroport/${aeroportId}`);
    return response.data;
  },

  create: async (carte: Carte): Promise<Carte> => {
    const response = await api.post('/cartes', carte);
    return response.data;
  },

  update: async (id: number, carte: Partial<Carte>): Promise<Carte> => {
    const response = await api.put(`/cartes/${id}`, carte);
    return response.data;
  },

  delete: async (id: number): Promise<void> => {
    await api.delete(`/cartes/${id}`);
  },
};

// Services pour les documents
export const documentService = {
  getAll: async (): Promise<Document[]> => {
    const response = await api.get('/documents');
    return response.data;
  },

  getById: async (id: number): Promise<Document> => {
    const response = await api.get(`/documents/${id}`);
    return response.data;
  },

  getByAeroport: async (aeroportId: number): Promise<Document[]> => {
    const response = await api.get(`/documents/aeroport/${aeroportId}`);
    return response.data;
  },

  create: async (document: Document): Promise<Document> => {
    const response = await api.post('/documents', document);
    return response.data;
  },

  update: async (id: number, document: Partial<Document>): Promise<Document> => {
    const response = await api.put(`/documents/${id}`, document);
    return response.data;
  },

  delete: async (id: number): Promise<void> => {
    await api.delete(`/documents/${id}`);
  },
};

// Services pour les informations
export const informationService = {
  getAll: async (): Promise<Information[]> => {
    const response = await api.get('/informations');
    return response.data;
  },

  getById: async (id: number): Promise<Information> => {
    const response = await api.get(`/informations/${id}`);
    return response.data;
  },

  getByAeroport: async (aeroportId: number): Promise<Information[]> => {
    const response = await api.get(`/informations/aeroport/${aeroportId}`);
    return response.data;
  },

  create: async (information: Information): Promise<Information> => {
    const response = await api.post('/informations', information);
    return response.data;
  },

  update: async (id: number, information: Partial<Information>): Promise<Information> => {
    const response = await api.put(`/informations/${id}`, information);
    return response.data;
  },

  delete: async (id: number): Promise<void> => {
    await api.delete(`/informations/${id}`);
  },
};

// Service de santé de l'API
export const healthCheck = async (): Promise<any> => {
  const response = await api.get('/health');
  return response.data;
};

export default api;

