'use server';

import { ResultDto } from '@/types/response/common';
import {
	PaperLeaderboardResponse,
	PaperLeaderboardSort,
	PaperLeaderboardPeriod,
	PaperTrackRecordResponse
} from '@/types/paper';
import { fetchJson } from '@/lib/utils/server';

// 리더보드 (익명 허용 · SSR). sort: return|winrate|mdd, period: week|month|all
export async function fetchPaperLeaderboard(params: { sort?: PaperLeaderboardSort; period?: PaperLeaderboardPeriod; page?: number }): Promise<ResultDto<PaperLeaderboardResponse>>
{
	const queryParams = new URLSearchParams();

	if (params.sort) queryParams.set('sort', params.sort);
	if (params.period) queryParams.set('period', params.period);
	queryParams.set('page', String(params.page ?? 1));

	return await fetchJson<PaperLeaderboardResponse>(`/api/paper/leaderboard?${queryParams.toString()}`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}

// 프로필 모의투자 성적 (익명 허용 · 회원 SID 기준)
export async function fetchPaperTrackRecord(memberSID: string): Promise<ResultDto<PaperTrackRecordResponse>>
{
	return await fetchJson<PaperTrackRecordResponse>(`/api/paper/members/${encodeURIComponent(memberSID)}/track-record`, {
		method: 'GET',
		headers: { 'Accept': 'application/json' }
	});
}
