// API wrappers
const API = {
    baseUrl: '/api',

    async getNetwork(params = {}) {
        const query = new URLSearchParams(params).toString();
        const response = await fetch(`${this.baseUrl}/network/?${query}`);
        if (!response.ok) throw new Error(`Network request failed: ${response.status}`);
        return response.json();
    },

    async getAirports(params = {}) {
        const query = new URLSearchParams(params).toString();
        const response = await fetch(`${this.baseUrl}/airports/?${query}`);
        if (!response.ok) throw new Error(`Airports request failed: ${response.status}`);
        return response.json();
    },

    async getAirport(iata) {
        const response = await fetch(`${this.baseUrl}/airports/${iata}`);
        if (!response.ok) throw new Error(`Airport request failed: ${response.status}`);
        return response.json();
    },

    async getRoutes(params = {}) {
        const query = new URLSearchParams(params).toString();
        const response = await fetch(`${this.baseUrl}/routes/?${query}`);
        if (!response.ok) throw new Error(`Routes request failed: ${response.status}`);
        return response.json();
    },

    async getStats() {
        const response = await fetch(`${this.baseUrl}/stats/`);
        if (!response.ok) throw new Error(`Stats request failed: ${response.status}`);
        return response.json();
    },

    async getDiagnostics(iata) {
        const response = await fetch(`${this.baseUrl}/airports/${iata}/diagnostics`);
        if (!response.ok) throw new Error(`Diagnostics request failed: ${response.status}`);
        return response.json();
    },
};
