// 提醒 API

import { request, type ApiResponse } from './client';

/** 提醒类型 */
export interface Reminder {
  id: string;
  user_id: string;
  title: string;
  description: string;
  remind_at: string; // ISO 8601
  status: 'pending' | 'completed' | 'cancelled';
  created_at: string;
  completed_at?: string | null;
  repeat_type: 'none' | 'daily' | 'weekly' | 'monthly';
  session_id: string;
  notified: boolean;
}

/** 创建提醒请求 */
export interface CreateReminderRequest {
  title: string;
  description?: string;
  remind_at: string; // ISO 8601
  repeat_type?: string;
  session_id?: string;
}

/** 更新提醒请求 */
export interface UpdateReminderRequest {
  title?: string;
  description?: string;
  remind_at?: string;
  status?: 'pending' | 'completed' | 'cancelled';
  repeat_type?: string;
  session_id?: string;
}

/** 提醒列表响应 */
export interface ReminderListResponse {
  reminders: Reminder[];
  count: number;
}

/**
 * 获取用户的提醒列表
 * @param userId 用户 ID (可选，不传则用当前登录用户)
 * @param status 状态筛选 (pending/completed/cancelled)
 * @param limit 分页大小
 */
export async function listReminders(
  userId?: string,
  status?: string,
  limit = 50
): Promise<ApiResponse<ReminderListResponse>> {
  const params = new URLSearchParams();
  if (userId) params.set('user_id', userId);
  if (status) params.set('status', status);
  params.set('limit', String(limit));

  return request<ReminderListResponse>(`/reminders?${params.toString()}`);
}

/**
 * 创建新提醒
 */
export async function createReminder(data: CreateReminderRequest): Promise<ApiResponse<{ success: boolean; reminder: Reminder }>> {
  return request<{ success: boolean; reminder: Reminder }>('/reminders', {
    method: 'POST',
    body: data,
  });
}

/**
 * 更新提醒
 */
export async function updateReminder(
  id: string,
  data: UpdateReminderRequest
): Promise<ApiResponse<{ success: boolean; reminder: Reminder }>> {
  return request<{ success: boolean; reminder: Reminder }>(`/reminders/${id}`, {
    method: 'PUT',
    body: data,
  });
}

/**
 * 删除提醒
 */
export async function deleteReminder(id: string): Promise<ApiResponse<{ success: boolean }>> {
  return request<{ success: boolean }>(`/reminders/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 取消提醒 (等同于更新状态为 cancelled)
 */
export async function cancelReminder(id: string): Promise<ApiResponse<{ success: boolean; reminder: Reminder }>> {
  return updateReminder(id, { status: 'cancelled' });
}
