import { CloudUser, CloudObject,CloudQuery } from './ncloud';

// 测试数据导入函数
export async function importTestData() {
  try {
    // 1. 创建或登录测试用户
    const user = new CloudUser();
    const testUsername = 'test_patient@111.com';
    const testPassword = 'test123456';
    
    // 尝试登录，如果失败则注册新用户
    let loggedInUser = await user.login(testUsername, testPassword);
    if (!loggedInUser) {
      loggedInUser = await user.signUp(testUsername, testPassword, {
        firstName: '张',
        lastName: '三',
        email: testUsername,
        birthDate: new Date('1970-05-15'),
        gender: 'male',
        medicalHistory: ['高血压', '2型糖尿病'],
        currentMedications: ['二甲双胍', '阿司匹林']
      });
      console.log('新用户注册成功:', loggedInUser);
    } else {
      console.log('用户登录成功:', loggedInUser);
    }
    //用户存在性检查：
    if (!loggedInUser) {
     throw new Error('无法创建或登录测试用户');
      }
    //测试删除
      await deleteUserHealthData(loggedInUser);
    // 2. 创建血糖测试数据
    const bloodGlucoseData = [
      { glucoseValue: 5.2, measurementType: '空腹', measurementTime: new Date('2025-05-29T08:00:00') },
      { glucoseValue: 7.8, measurementType: '餐后', measurementTime: new Date('2025-05-29T14:30:00') },
      { glucoseValue: 6.5, measurementType: '随机', measurementTime: new Date('2025-05-29T10:15:00') }
    ];

    for (const data of bloodGlucoseData) {
      const glucose = new CloudObject('BloodGlucose');
      glucose.set({
        patient: user.toPointer(),
        ...data,
        notes: '早餐后测量',
        tags: ['早餐后', '正常范围']
      });
      await glucose.save();
      console.log('血糖数据创建成功:', glucose.id);
    }

    // 3. 创建血压测试数据
    const bloodPressureData = [
      { systolic: 120, diastolic: 80, pulse: 72, measurementTime: new Date('2025-05-29T09:00:00'), measurementPosition: '坐姿' },
      { systolic: 135, diastolic: 85, pulse: 76, measurementTime: new Date('2025-05-29T09:00:00'), measurementPosition: '坐姿' },
      { systolic: 128, diastolic: 82, pulse: 74, measurementTime: new Date('2025-05-29T09:00:00'), measurementPosition: '坐姿' }
    ];

    for (const data of bloodPressureData) {
      const pressure = new CloudObject('BloodPressure');
      pressure.set({
        patient: user.toPointer(),
        ...data,
        notes: '早晨服药前测量',
        tags: ['早晨', '服药前']
      });
      await pressure.save();
      console.log('血压数据创建成功:', pressure.id);
    }

    // 4. 创建心率测试数据
    const heartRateData = [
      { heartRate: 72, measurementTime: new Date('2025-05-29T08:30:00'), measurementMethod: '设备', rhythm: '规律' },
      { heartRate: 68, measurementTime: new Date('2025-05-29T08:30:00'), measurementMethod: '设备', rhythm: '规律' },
      { heartRate: 75, measurementTime: new Date('2025-05-29T08:30:00'), measurementMethod: '手动', rhythm: '规律' }
    ];

    for (const data of heartRateData) {
      const heartRate = new CloudObject('HeartRate');
      heartRate.set({
        patient: user.toPointer(),
        ...data,
        notes: '静息心率测量',
        tags: ['静息', '正常']
      });
      await heartRate.save();
      console.log('心率数据创建成功:', heartRate.id);
    }

    console.log('所有测试数据导入完成！');
  } catch (error) {
    console.error('导入测试数据时出错:', error);
  }
}
async function deleteUserHealthData(user: CloudUser) {
  try {
    const classes = ['BloodGlucose', 'BloodPressure', 'HeartRate'];
    
    for (const className of classes) {
      const query = new CloudQuery(className);
      query.equalTo('patient', user.toPointer());
      const results = await query.find();
      
      // 逐条删除记录
      if (results.length > 0) {
        for (const record of results) {
          await record.destroy();
        }
        console.log(`已删除 ${results.length} 条 ${className} 记录`);
      }
    }
    return true;
  } catch (error) {
    console.error('删除用户数据时出错:', error);
    throw error;
  }
}
// 执行导入
importTestData();