/**
 * API Configuration - URLs e funções de requisição
 */

// URL base da API - usa path relativo para passar pelo middleware do Next.js
// que adiciona os headers de tenant (X-Tenant-Subdomain)
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || '';

/**
 * Endpoints da API
 */
export const API_ENDPOINTS = {
    // Auth
    register: `${API_BASE_URL}/api/auth/register`,
    login: `${API_BASE_URL}/api/auth/login`,
    logout: `${API_BASE_URL}/api/auth/logout`,
    refresh: `${API_BASE_URL}/api/auth/refresh`,
    me: `${API_BASE_URL}/api/me`,

    // Admin / Agencies
    adminAgencyRegister: `${API_BASE_URL}/api/admin/agencies/register`,
    agencyProfile: `${API_BASE_URL}/api/agency/profile`,
    tenantConfig: `${API_BASE_URL}/api/tenant/config`,

    // Health
    health: `${API_BASE_URL}/health`,
    apiHealth: `${API_BASE_URL}/api/health`,
} as const;

/**
 * Wrapper para fetch com tratamento de erros
 */
export async function apiRequest<T = any>(
    url: string,
    options?: RequestInit
): Promise<T> {
    try {
        const response = await fetch(url, {
            ...options,
            headers: {
                'Content-Type': 'application/json',
                ...options?.headers,
            },
        });

        // Handle empty responses
        const text = await response.text();
        let data: any = {};

        if (text) {
            try {
                data = JSON.parse(text);
            } catch (e) {
                // If not JSON but has text, might be an error or plain text
                data = { message: text };
            }
        }

        if (!response.ok) {
            throw new Error(data.message || `Erro ${response.status}`);
        }

        return data as T;
    } catch (error) {
        if (error instanceof Error) {
            throw error;
        }
        throw new Error('Erro desconhecido na requisição');
    }
}
