import { headers } from 'next/headers';

export interface BrandingData {
    primary_color: string;
    logo_url?: string;
    logo_horizontal_url?: string;
    name: string;
}

export async function getBranding(): Promise<BrandingData> {
    try {
        const headersList = await headers();
        const hostname = headersList.get('host') || '';
        const subdomain = hostname.split('.')[0];

        console.log(`[getBranding] Buscando branding para subdomain: ${subdomain}`);

        const response = await fetch(`http://aggios-backend:8080/api/tenant/config?subdomain=${subdomain}`, {
            cache: 'no-store',
            headers: {
                'Content-Type': 'application/json',
            },
        });

        if (!response.ok) {
            console.error(`[getBranding] Erro: ${response.status}`);
            return {
                primary_color: '#6366f1',
                name: 'Agência',
            };
        }

        const data = await response.json();
        console.log(`[getBranding] Dados recebidos:`, data);

        return {
            primary_color: data.primary_color || '#6366f1',
            logo_url: data.logo_url,
            logo_horizontal_url: data.logo_horizontal_url,
            name: data.name || 'Agência',
        };
    } catch (error) {
        console.error('[getBranding] Erro:', error);
        return {
            primary_color: '#6366f1',
            name: 'Agência',
        };
    }
}
