import '../core/api_client.dart';

/// 健康数据服务
class HealthService {
  final ApiClient _api;
  HealthService(this._api);

  /// 获取各指标最新值
  Future<Map<String, dynamic>> getLatest() async {
    final res = await _api.get('/api/health-records/latest');
    return res.data['data'] ?? {};
  }

  /// 获取趋势数据
  Future<List<Map<String, dynamic>>> getTrend(String type, {int period = 7}) async {
    final res = await _api.get('/api/health-records/trend', queryParameters: {'type': type, 'period': period});
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  /// 获取记录列表
  Future<List<Map<String, dynamic>>> getRecords({String? type, int? days}) async {
    final params = <String, dynamic>{};
    if (type != null) params['type'] = type;
    if (days != null) params['days'] = days;
    final res = await _api.get('/api/health-records', queryParameters: params);
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }
}

/// 用户服务
class UserService {
  final ApiClient _api;
  UserService(this._api);

  Future<Map<String, dynamic>?> getProfile() async {
    final res = await _api.get('/api/user/profile');
    return res.data['data'];
  }

  Future<void> updateProfile({String? name, String? gender, String? birthDate}) async {
    await _api.put('/api/user/profile', data: {'name': name, 'gender': gender, 'birthDate': birthDate});
  }

  Future<Map<String, dynamic>?> getHealthArchive() async {
    final res = await _api.get('/api/user/health-archive');
    return res.data['data'];
  }

  Future<void> updateHealthArchive(Map<String, dynamic> data) async {
    await _api.put('/api/user/health-archive', data: data);
  }

  Future<void> deleteAccount() async {
    await _api.delete('/api/user/account');
  }
}

/// 用药服务
class MedicationService {
  final ApiClient _api;
  MedicationService(this._api);

  Future<List<Map<String, dynamic>>> getList() async {
    final res = await _api.get('/api/medications');
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<void> create(Map<String, dynamic> data) async {
    await _api.post('/api/medications', data: data);
  }

  Future<void> update(String id, Map<String, dynamic> data) async {
    await _api.put('/api/medications/$id', data: data);
  }

  Future<void> delete(String id) async {
    await _api.delete('/api/medications/$id');
  }

  Future<List<Map<String, dynamic>>> getMedications(String filter) async {
    final params = <String, dynamic>{};
    if (filter.isNotEmpty) params['filter'] = filter;
    final res = await _api.get('/api/medications', queryParameters: params.isNotEmpty ? params : null);
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<void> deleteMedication(String id) async {
    await _api.delete('/api/medications/$id');
  }

  Future<void> confirm(String id) async {
    await _api.post('/api/medications/$id/confirm');
  }

  Future<List<Map<String, dynamic>>> getReminders() async {
    final res = await _api.get('/api/medications/reminders');
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }
}

/// 饮食服务
class DietService {
  final ApiClient _api;
  DietService(this._api);

  Future<List<Map<String, dynamic>>> getRecords({String? date, String? mealType}) async {
    final params = <String, dynamic>{};
    if (date != null) params['date'] = date;
    if (mealType != null) params['mealType'] = mealType;
    final res = await _api.get('/api/diet-records', queryParameters: params);
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<void> create(Map<String, dynamic> data) async {
    await _api.post('/api/diet-records', data: data);
  }

  Future<void> deleteRecord(String id) async {
    await _api.delete('/api/diet-records/$id');
  }
}

/// 问诊服务
class ConsultationService {
  final ApiClient _api;
  ConsultationService(this._api);

  Future<List<Map<String, dynamic>>> getDoctors() async {
    final res = await _api.get('/api/doctors');
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<Map<String, dynamic>> getQuota() async {
    final res = await _api.get('/api/user/consultation-quota');
    return res.data['data'] ?? {};
  }

  Future<Map<String, dynamic>> createConsultation(String doctorId) async {
    final res = await _api.post('/api/consultations', data: {'doctorId': doctorId});
    return res.data['data'];
  }

  Future<List<Map<String, dynamic>>> getMessages(String consultationId, {String? after}) async {
    final params = <String, dynamic>{};
    if (after != null) params['after'] = after;
    final res = await _api.get('/api/consultations/$consultationId/messages', queryParameters: params);
    final list = res.data['data'] as List? ?? [];
    return list.cast<Map<String, dynamic>>();
  }

  Future<void> sendMessage(String consultationId, String content) async {
    await _api.post('/api/consultations/$consultationId/messages', data: {'content': content});
  }
}

/// 运动服务
class ExerciseService {
  final ApiClient _api;
  ExerciseService(this._api);

  Future<Map<String, dynamic>?> getCurrentPlan() async {
    final res = await _api.get('/api/exercise-plans/current');
    return res.data['data'];
  }

  Future<void> createPlan(Map<String, dynamic> data) async {
    await _api.post('/api/exercise-plans', data: data);
  }

  Future<void> createPlanSimple(Map<String, dynamic> data) async {
    await _api.post('/api/exercise-plans', data: data);
  }

  Future<void> deletePlan(String id) async {
    await _api.delete('/api/exercise-plans/$id');
  }

  Future<void> checkIn(String itemId) async {
    await _api.post('/api/exercise-plans/items/$itemId/checkin');
  }

  Future<List<Map<String, dynamic>>> getPlans() async {
    final res = await _api.get('/api/exercise-plans');
    return (res.data['data'] as List?)?.cast<Map<String, dynamic>>() ?? [];
  }

  Future<Map<String, dynamic>?> getPlanDetail(String id) async {
    final res = await _api.get('/api/exercise-plans/$id');
    return res.data['data'];
  }
}
