﻿import type { ReactNode } from 'react';
import { headers } from 'next/headers';
import {
    resolveRequestHostFromHeaders,
    runWithRequestHost,
} from '@waygest/api-tenant';
import { VerticalAppProviders } from '@waygest/vertical-app-kit';
import { WaygestTelemetryBoot } from '@/components/WaygestTelemetryBoot';

export const dynamic = 'force-dynamic';

/** Layout tipológico Software (ADR-0118) — sem chrome ERP. */
export default async function RootLayout({ children }: { children: ReactNode }) {
    const headersList = await headers();
    const host = resolveRequestHostFromHeaders(headersList);
    const proto = (headersList.get('x-forwarded-proto') === 'https' ? 'https' : 'http') as
        | 'http'
        | 'https';

    return runWithRequestHost(
        host,
        () => (
            <html lang="pt-BR">
                <head>
                    {/* Âncora managed: VerticalBrandingHead substitui pelo favicon do tenant (O1-010). */}
                    <link rel="icon" href="data:," data-waygest-managed="1" />
                </head>
                <body style={{ margin: 0 }}>
                    <WaygestTelemetryBoot>
                        <VerticalAppProviders>{children}</VerticalAppProviders>
                    </WaygestTelemetryBoot>
                </body>
            </html>
        ),
        proto,
    );
}
