import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:health_app/core/app_router.dart';
import 'package:health_app/core/navigation_provider.dart';
import 'package:health_app/pages/report/report_pages.dart';
import 'package:health_app/pages/doctor/doctor_followup_edit_page.dart';
import 'package:health_app/pages/admin/admin_add_doctor_page.dart';

void main() {
  testWidgets(
    'detail routes show an error instead of throwing when id is missing',
    (tester) async {
      await tester.pumpWidget(
        ProviderScope(
          child: Consumer(
            builder: (context, ref, _) {
              return MaterialApp(
                home: buildPage(const RouteInfo('aiAnalysis'), ref),
              );
            },
          ),
        ),
      );

      expect(tester.takeException(), isNull);
      expect(find.text('页面参数错误'), findsOneWidget);
    },
  );

  testWidgets('report upload route opens report manager upload options', (
    tester,
  ) async {
    late Widget page;
    await tester.pumpWidget(
      ProviderScope(
        child: Consumer(
          builder: (context, ref, _) {
            page = buildPage(
              const RouteInfo('reports', params: {'openUpload': 'true'}),
              ref,
            );
            return const MaterialApp(home: SizedBox());
          },
        ),
      ),
    );

    expect(page, isA<ReportListPage>());
    expect((page as ReportListPage).openUploadOnEnter, isTrue);
  });

  testWidgets('follow-up editor route supports create mode without an id', (
    tester,
  ) async {
    late Widget page;
    await tester.pumpWidget(
      ProviderScope(
        child: Consumer(
          builder: (context, ref, _) {
            page = buildPage(const RouteInfo('doctorFollowUpEdit'), ref);
            return const MaterialApp(home: SizedBox());
          },
        ),
      ),
    );

    expect(page, isA<DoctorFollowUpEditPage>());
    expect((page as DoctorFollowUpEditPage).id, isNull);
  });

  testWidgets('admin doctor form route supports edit mode', (tester) async {
    late Widget page;
    await tester.pumpWidget(
      ProviderScope(
        child: Consumer(
          builder: (context, ref, _) {
            page = buildPage(
              const RouteInfo(
                'adminAddDoctor',
                params: {
                  'id': 'doctor-1',
                  'phone': '13800138000',
                  'name': '王医生',
                },
              ),
              ref,
            );
            return const MaterialApp(home: SizedBox());
          },
        ),
      ),
    );

    expect(page, isA<AdminAddDoctorPage>());
    expect((page as AdminAddDoctorPage).id, 'doctor-1');
    expect((page as AdminAddDoctorPage).initialName, '王医生');
  });
}
