import DashboardPage from "@/components/user/dashboard/dashboardPage";
import { getMyAllPosts } from "@/libs/axios/modules/post";
import { getTransactionNovel } from "@/libs/axios/modules/transaction";
import { getProfile } from "@/libs/axios/modules/user";
import { fetchCoinHistoryPageServer } from "@/libs/query/coinHistoryQuery";
import { PostModel, TransactionModel } from "@/models";
import { cookies } from "next/headers";

type CoinHistoryItem = {
    id: string;
    title: string;
    amount: number;
    created_at: string;
    status?: string;
};

type TopupHistory = { items: CoinHistoryItem[]; hasMore: boolean };

export default async function Dashboard() {
    const cookieStore = await cookies();
    const rawToken = cookieStore.get("token")!.value.slice(1, -1);

    const [postsRes, transactionRes, profileRes, topupHistory] = await Promise.all([
        getMyAllPosts(rawToken),
        getTransactionNovel(rawToken, { page: 1, limit: 10 }),
        getProfile(rawToken),
        fetchCoinHistoryPageServer({ token: rawToken, type: "topup", page: 1, limit: 10 }),
    ]);

    return (
        <DashboardPage
            myAllPosts={postsRes.data.data as PostModel[]}
            transactionNovel={transactionRes.data.data as TransactionModel[]}
            topupHistory={topupHistory as TopupHistory}
            user={profileRes.data.data}
            token={rawToken}
        />
    );
}