import { notFound } from 'next/navigation';
import { getProfile } from '@/actions/getProfile';
import { getSettings } from '@/actions/getSettings';
import { ProfileForm } from './ProfileForm';

export type SettingsPageProps = PageProps<'/[locale]/profile/[token]/settings'>;

export default async function SettingsPage(props: SettingsPageProps) {
    const params = await props.params;
    const searchParams = await props.searchParams;

    const [settings, profile] = await Promise.all([
        getSettings({
            IPCountry:
                typeof searchParams.IPCountry === 'string'
                    ? searchParams.IPCountry
                    : undefined,
        }),
        getProfile(params.token),
    ]);

    if (profile.error) {
        if (profile.error.type === 'PAGE_NOT_FOUND') {
            return notFound();
        } else {
            throw profile.error.message;
        }
    }

    if (settings.error) {
        throw settings.error.message;
    }

    return (
        <ProfileForm
            defaultValues={profile.data}
            isDateOfBirthAllowed={settings.data?.isDateOfBirthAllowed}
        />
    );
}
