'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,
    getUserData
} 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>();
    const [isFull, setIsFull] = useState<boolean>(false);

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

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