import { api } from './api'; export type ChallengeStatus = 'upcoming' | 'ongoing' | 'expired'; export type ChallengeProgressDto = { completed: number; target: number; remaining: number checkedInToday: boolean; }; export type RankingItemDto = { id: string; name: string; avatar: string | null; metric: string; badge?: string; todayReportedValue?: number; todayTargetValue?: number; }; export enum ChallengeType { WATER = 'water', EXERCISE = 'exercise', DIET = 'diet', MOOD = 'mood', SLEEP = 'sleep', WEIGHT = 'weight', } export type ChallengeListItemDto = { id: string; title: string; image: string; periodLabel?: string; durationLabel: string; requirementLabel: string; unit?: string; status: ChallengeStatus; participantsCount: number; rankingDescription?: string; highlightTitle: string; highlightSubtitle: string; ctaLabel: string; progress?: ChallengeProgressDto; isJoined: boolean; startAt?: string; endAt?: string; minimumCheckInDays: number; // 最小打卡天数 type: ChallengeType; }; export type ChallengeDetailDto = ChallengeListItemDto & { summary?: string; rankings: RankingItemDto[]; userRank?: number; }; export type ChallengeRankingsDto = { total: number; page: number; pageSize: number; items: RankingItemDto[]; }; export async function listChallenges(): Promise { return api.get('/challenges'); } export async function getChallengeDetail(id: string): Promise { return api.get(`/challenges/${encodeURIComponent(id)}`); } export async function joinChallenge(id: string): Promise { return api.post(`/challenges/${encodeURIComponent(id)}/join`); } export async function leaveChallenge(id: string): Promise { return api.post(`/challenges/${encodeURIComponent(id)}/leave`); } export async function reportChallengeProgress(id: string, value?: number): Promise { const body = value != null ? { value } : undefined; return api.post(`/challenges/${encodeURIComponent(id)}/progress`, body); } export async function getChallengeRankings( id: string, params?: { page?: number; pageSize?: number } ): Promise { const searchParams = new URLSearchParams(); if (params?.page) { searchParams.append('page', String(params.page)); } if (params?.pageSize) { searchParams.append('pageSize', String(params.pageSize)); } const query = searchParams.toString(); const url = `/challenges/${encodeURIComponent(id)}/rankings${query ? `?${query}` : ''}`; return api.get(url); }