import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:health_app/models/ble_device.dart';
import 'package:health_app/pages/chart/trend_page.dart';
import 'package:health_app/pages/remaining_pages.dart';
import 'package:health_app/services/health_ble_service.dart';

void main() {
  test('unknown trend metric falls back to blood pressure', () {
    expect(normalizeTrendMetricType('unknown_metric'), 'blood_pressure');
    expect(normalizeTrendMetricType(null), 'blood_pressure');
    expect(normalizeTrendMetricType('spo2'), 'spo2');
  });

  test('trend period includes today and the requested preceding days', () {
    final now = DateTime(2026, 7, 13, 18);
    final records = [
      {'date': DateTime(2026, 7, 6, 23), 'value': 70},
      {'date': DateTime(2026, 7, 7, 0), 'value': 71},
      {'date': DateTime(2026, 7, 13, 8), 'value': 72},
    ];

    final result = filterTrendRecordsForPeriod(records, 7, now);

    expect(result.map((record) => record['value']), [71, 72]);
  });

  test('trend values do not show an unnecessary decimal', () {
    expect(formatTrendNumber(88.0), '88');
    expect(formatTrendNumber(88.5), '88.5');
    expect(formatTrendNumber(null), '--');
  });

  test('blood pressure abnormal label identifies the affected value', () {
    expect(
      trendStatusLabel({
        'systolic': 113,
        'diastolic': 97,
        'isAbnormal': true,
      }, 'blood_pressure'),
      '舒张压偏高',
    );
    expect(
      trendStatusLabel({
        'systolic': 122,
        'diastolic': 89,
        'isAbnormal': false,
      }, 'blood_pressure'),
      '',
    );
  });

  test('calendar month switch keeps the selected day when possible', () {
    expect(
      calendarDateForMonth(DateTime(2026, 6), DateTime(2026, 7, 13)),
      DateTime(2026, 6, 13),
    );
    expect(
      calendarDateForMonth(DateTime(2026, 2), DateTime(2026, 1, 31)),
      DateTime(2026, 2, 28),
    );
  });

  test('health archive list input is normalized without conflicting none', () {
    expect(normalizeArchiveItems('青霉素，海鲜\n花粉'), ['青霉素', '海鲜', '花粉']);
    expect(normalizeArchiveItems('无'), ['无']);
    expect(normalizeArchiveItems('无、青霉素'), ['青霉素']);
  });

  test('static compliance pages include collection and sdk lists', () {
    expect(staticTextTitle('personalInfoList'), '个人信息收集清单');
    expect(staticTextContent('personalInfoList'), contains('健康数据'));

    expect(staticTextTitle('thirdPartySdkList'), '第三方 SDK 共享清单');
    expect(staticTextContent('thirdPartySdkList'), contains('AI'));
  });

  test('all in-app compliance documents contain readable Chinese', () {
    final source = File('lib/pages/remaining_pages.dart').readAsStringSync();
    final start = source.indexOf(
      'const Map<String, String> _extraStaticTextTitles',
    );
    expect(start, isNonNegative);
    final complianceSection = source.substring(start);

    expect(complianceSection, isNot(contains('锛')));
    expect(complianceSection, isNot(contains('銆')));
    expect(complianceSection, isNot(contains('€')));
  });

  test('ble sync is only implemented for blood pressure devices', () {
    expect(
      HealthBleService.isSyncImplemented(BleDeviceType.bloodPressure),
      true,
    );
    expect(HealthBleService.isSyncImplemented(BleDeviceType.glucose), false);
    expect(
      HealthBleService.isSyncImplemented(BleDeviceType.weightScale),
      false,
    );
    expect(
      HealthBleService.isSyncImplemented(BleDeviceType.pulseOximeter),
      false,
    );
  });
}
