import { getBranding } from '@/lib/branding';
import SucessoClient from './sucesso-client';

const lightenColor = (hexColor: string, amount = 20) => {
    const fallback = '#3b82f6';
    if (!hexColor) return fallback;

    let color = hexColor.replace('#', '');
    if (color.length === 3) {
        color = color.split('').map(char => char + char).join('');
    }
    if (color.length !== 6) return fallback;

    const num = parseInt(color, 16);
    if (Number.isNaN(num)) return fallback;

    const clamp = (value: number) => Math.max(0, Math.min(255, value));
    const r = clamp((num >> 16) + amount);
    const g = clamp(((num >> 8) & 0x00ff) + amount);
    const b = clamp((num & 0x0000ff) + amount);

    return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
};

export default async function CadastroSucessoPage() {
    const branding = await getBranding();
    const primaryColor = branding.primary_color || '#3b82f6';
    const accentColor = lightenColor(primaryColor, 30);
    const now = new Date();
    const submittedAt = now.toLocaleString('pt-BR', {
        day: '2-digit',
        month: 'long',
        year: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
    });

    return (
        <SucessoClient
            branding={{
                name: branding.name,
                logo_url: branding.logo_url,
                primary_color: primaryColor
            }}
            accentColor={accentColor}
            submittedAt={submittedAt}
        />
    );
}
