'use client'

import { useEffect, useState } from 'react';
import { message } from 'antd';
// import { unstable_setRequestLocale } from 'next-intl/server';
import { ExpertData } from '../../../../../types/profile';
import { AUTH_TOKEN_KEY } from '../../../../../constants/common';
import { useLocalStorage } from '../../../../../hooks/useLocalStorage';
import { getEducation, getPersonalData, getTags, getPractice, getSchedule, getPayData } from '../../../../../actions/profile';
import { ExpertProfile } from '../../../../../components/ExpertProfile';
import { Loader } from '../../../../../components/view/Loader';

export default function ExpertProfilePage({ params: { locale } }: { params: { locale: string } }) {
    // unstable_setRequestLocale(locale);
    const [jwt] = useLocalStorage(AUTH_TOKEN_KEY, '');
    const [loading, setLoading] = useState<boolean>(false);
    const [data, setData] = useState<ExpertData | undefined>();

    useEffect(() => {
        if (jwt) {
            setLoading(true);
            Promise.all([
                getPersonalData(locale, jwt),
                getEducation(locale, jwt),
                getTags(locale, jwt),
                getPractice(locale, jwt),
                getSchedule(locale, jwt),
                getPayData(locale, jwt)
            ])
                .then(([person, education, tags, practice, schedule, payData]) => {
                    console.log('person', person);
                    console.log('education', education);
                    console.log('tags', tags);
                    console.log('practice', practice);
                    console.log('schedule', schedule);
                    console.log('payData', payData);
                    setData({
                        person,
                        education,
                        tags,
                        practice,
                        schedule,
                        payData
                    });
                })
                .catch(() => {
                    message.error('Не удалось загрузить данные эксперта');
                })
                .finally(() => {
                    setLoading(false);
                })
        }
    }, [jwt]);

    return (
        <Loader isLoading={loading}>
            {data && (
                <ExpertProfile
                    locale={locale}
                    data={data}
                    updateData={setData}
                />
            )}
        </Loader>
    );
};
