/**
 * 家人端 - 日程管理服务
 * 提供护理计划的增删改查功能
 */

const API_BASE_URL = 'http://localhost:8000/api';

export interface Schedule {
  id?: number;
  family_id: string;
  title: string;
  description?: string;
  schedule_type?: 'medication' | 'exercise' | 'meal' | 'checkup' | 'other';
  schedule_time: string; // ISO 8601 格式
  repeat_type?: 'once' | 'daily' | 'weekly' | 'monthly';
  repeat_days?: string; // JSON 字符串，如 "[1,3,5]"
  status?: 'pending' | 'completed' | 'skipped' | 'missed';
  completed_at?: string;
  auto_remind?: number; // 数字人自动播报：1=启用，0=禁用
  is_active?: number;
  created_by?: number;
  created_at?: string;
  updated_at?: string;
  creator_name?: string;
}

export interface ScheduleResponse {
  schedules: Schedule[];
}

export interface CreateScheduleResponse {
  success: boolean;
  schedule_id: number;
}

export interface UpdateScheduleResponse {
  success: boolean;
}

/**
 * 获取家庭所有日程
 */
export async function getFamilySchedules(familyId: string): Promise<Schedule[]> {
  try {
    const response = await fetch(
      `${API_BASE_URL}/family/schedules?family_id=${familyId}`
    );

    if (!response.ok) {
      throw new Error(`获取日程失败: ${response.statusText}`);
    }

    const data: ScheduleResponse = await response.json();
    return data.schedules || [];
  } catch (error) {
    console.error('获取日程错误:', error);
    throw error;
  }
}

/**
 * 创建新日程
 */
export async function createSchedule(schedule: Schedule): Promise<number> {
  try {
    const response = await fetch(`${API_BASE_URL}/family/schedules`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(schedule),
    });

    if (!response.ok) {
      throw new Error(`创建日程失败: ${response.statusText}`);
    }

    const data: CreateScheduleResponse = await response.json();
    return data.schedule_id;
  } catch (error) {
    console.error('创建日程错误:', error);
    throw error;
  }
}

/**
 * 更新日程
 */
export async function updateSchedule(
  scheduleId: number,
  updates: Partial<Schedule>
): Promise<boolean> {
  try {
    const response = await fetch(
      `${API_BASE_URL}/family/schedules/${scheduleId}`,
      {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(updates),
      }
    );

    if (!response.ok) {
      throw new Error(`更新日程失败: ${response.statusText}`);
    }

    const data: UpdateScheduleResponse = await response.json();
    return data.success;
  } catch (error) {
    console.error('更新日程错误:', error);
    throw error;
  }
}

/**
 * 删除日程（软删除）
 */
export async function deleteSchedule(scheduleId: number): Promise<boolean> {
  try {
    const response = await fetch(
      `${API_BASE_URL}/family/schedules/${scheduleId}`,
      {
        method: 'DELETE',
      }
    );

    if (!response.ok) {
      throw new Error(`删除日程失败: ${response.statusText}`);
    }

    const data: UpdateScheduleResponse = await response.json();
    return data.success;
  } catch (error) {
    console.error('删除日程错误:', error);
    throw error;
  }
}

/**
 * 格式化日期时间为 ISO 字符串
 */
export function formatDateTime(date: Date): string {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * 获取日程类型的显示名称
 */
export function getScheduleTypeLabel(type: string): string {
  const labels: Record<string, string> = {
    medication: '用药',
    exercise: '运动',
    meal: '饮食',
    checkup: '检查',
    other: '其他',
  };
  return labels[type] || type;
}

/**
 * 获取重复类型的显示名称
 */
export function getRepeatTypeLabel(type: string): string {
  const labels: Record<string, string> = {
    once: '单次',
    daily: '每天',
    weekly: '每周',
    monthly: '每月',
  };
  return labels[type] || type;
}

/**
 * 解析重复日期（星期几）
 */
export function parseRepeatDays(repeatDays: string): number[] {
  try {
    return JSON.parse(repeatDays || '[]');
  } catch {
    return [];
  }
}

/**
 * 格式化重复日期为字符串
 */
export function formatRepeatDays(days: number[]): string {
  return JSON.stringify(days);
}

/**
 * 获取星期几的显示名称
 */
export function getWeekdayLabel(day: number): string {
  const labels = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  return labels[day] || '';
}
