import { api } from '@/services/api';

export type CheckinMetrics = Record<string, any>;

export type CreateCheckinDto = {
  workoutId?: string;
  planId?: string;
  title?: string;
  checkinDate?: string; // YYYY-MM-DD
  startedAt?: string; // ISO
  notes?: string;
  metrics?: CheckinMetrics;
};

export type UpdateCheckinDto = Partial<CreateCheckinDto> & {
  id: string;
  status?: string; // 由后端验证为 CheckinStatus
  completedAt?: string; // ISO
  durationSeconds?: number;
};

export type CompleteCheckinDto = {
  id: string;
  completedAt?: string; // ISO
  durationSeconds?: number;
  notes?: string;
  metrics?: CheckinMetrics;
};

export type RemoveCheckinDto = {
  id: string;
};

export type SyncCheckinDto = {
  date: string; // YYYY-MM-DD
  items: any[]; // CheckinExercise[]
  note?: string;
  id?: string;
};

export async function createCheckin(dto: CreateCheckinDto): Promise<{ id: string } & Record<string, any>> {
  return await api.post('/api/checkins/create', dto);
}

export async function updateCheckin(dto: UpdateCheckinDto): Promise<{ id: string } & Record<string, any>> {
  return await api.put('/api/checkins/update', dto);
}

export async function completeCheckin(dto: CompleteCheckinDto): Promise<{ id: string } & Record<string, any>> {
  return await api.post('/api/checkins/complete', dto);
}

export async function removeCheckin(dto: RemoveCheckinDto): Promise<boolean | Record<string, any>> {
  return await api.delete('/api/checkins/remove', { body: dto });
}

// 按天获取打卡列表（后端默认今天，不传 date 也可）
export async function fetchDailyCheckins(date?: string): Promise<any[]> {
  const path = date ? `/api/checkins/daily?date=${encodeURIComponent(date)}` : '/api/checkins/daily';
  const data = await api.get<any[]>(path);
  // 返回值兼容 { data: T } 或直接 T 已由 api 封装处理
  return Array.isArray(data) ? data : [];
}

// 优先尝试按区间批量获取（若后端暂未实现将抛错，由调用方回退到逐日请求）
export async function fetchCheckinsInRange(startDate: string, endDate: string): Promise<any[]> {
  const path = `/api/checkins/range?start=${encodeURIComponent(startDate)}&end=${encodeURIComponent(endDate)}`;
  const data = await api.get<any[]>(path);
  return Array.isArray(data) ? data : [];
}

// 获取时间范围内每日是否已打卡（仅返回日期+布尔）
export type DailyStatusItem = { date: string; checkedIn: boolean };
export async function fetchDailyStatusRange(startDate: string, endDate: string): Promise<DailyStatusItem[]> {
  const path = `/api/checkins/range/daily-status?startDate=${encodeURIComponent(startDate)}&endDate=${encodeURIComponent(endDate)}`;
  const data = await api.get<DailyStatusItem[]>(path);
  return Array.isArray(data) ? data : [];
}

// 同步打卡数据到服务器
export async function syncCheckin(dto: SyncCheckinDto): Promise<{ id: string } & Record<string, any>> {
  console.log('dto', dto);
  
  return await api.post('/api/checkins/create', dto);
}


