import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:health_app/pages/settings/notification_prefs_logic.dart';

void main() {
  test('notification preferences parse the complete backend payload', () {
    final prefs = NotificationPrefs.fromJson({
      'pushEnabled': false,
      'medicationReminder': false,
      'followUpReminder': true,
      'doctorReply': true,
      'abnormalAlert': false,
      'dndEnabled': true,
      'dndStartMinutes': 1200,
      'dndEndMinutes': 420,
      'healthRecordReminder': true,
      'healthRecordReminderBloodPressure': true,
      'healthRecordReminderHeartRate': false,
      'healthRecordReminderGlucose': true,
      'healthRecordReminderSpO2': false,
      'healthRecordReminderWeight': false,
    });

    expect(prefs.pushEnabled, false);
    expect(prefs.doctorReply, true);
    expect(prefs.dndStartMinutes, 1200);
    expect(prefs.enabledHealthMetrics, {
      HealthReminderMetric.bloodPressure,
      HealthReminderMetric.glucose,
    });
  });

  test('metric summary remains compact and readable', () {
    expect(
      healthMetricSummary({
        HealthReminderMetric.bloodPressure,
        HealthReminderMetric.heartRate,
      }),
      '血压、心率',
    );
    expect(healthMetricSummary(HealthReminderMetric.values.toSet()), '全部指标');
    expect(healthMetricSummary({}), '至少选择一项');
  });

  test('metric payload sends all five fields in one request', () {
    final payload = healthMetricUpdatePayload({
      HealthReminderMetric.bloodPressure,
      HealthReminderMetric.spo2,
    });

    expect(payload.length, 5);
    expect(payload['healthRecordReminderBloodPressure'], true);
    expect(payload['healthRecordReminderHeartRate'], false);
    expect(payload['healthRecordReminderSpO2'], true);
  });

  test('notification settings uses a batch metric bottom sheet', () {
    final source = File(
      'lib/pages/settings/notification_prefs_page.dart',
    ).readAsStringSync();

    expect(source, contains('showModalBottomSheet'));
    expect(source, contains('saveHealthMetrics'));
    expect(source, contains('提醒指标'));
  });
}
