import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/app_colors.dart';
import '../../widgets/doctor_drawer.dart';
import 'doctor_dashboard_page.dart';
import 'doctor_patients_page.dart';
import 'doctor_consultations_page.dart';
import 'doctor_reports_page.dart';
import 'doctor_followups_page.dart';

class DoctorHomePage extends ConsumerWidget {
  const DoctorHomePage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final page = ref.watch(doctorPageProvider);

    return PopScope(
      canPop: page == 'dashboard',
      onPopInvokedWithResult: (didPop, _) {
        if (!didPop) ref.read(doctorPageProvider.notifier).set('dashboard');
      },
      child: Scaffold(
        backgroundColor: AppColors.background,
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
          surfaceTintColor: Colors.transparent,
          title: Text(
            _titleFor(page),
            style: const TextStyle(color: AppColors.textPrimary),
          ),
          leading: Builder(
            builder: (ctx) => IconButton(
              icon: const Icon(Icons.menu, color: AppColors.textPrimary),
              onPressed: () => Scaffold.of(ctx).openDrawer(),
            ),
          ),
        ),
        drawer: const DoctorDrawer(),
        body: _bodyFor(page),
      ),
    );
  }

  Widget _bodyFor(String page) => switch (page) {
    'dashboard' => const DoctorDashboardPage(),
    'patients' => const DoctorPatientsPage(),
    'consultations' => const DoctorConsultationsPage(),
    'reports' => const DoctorReportsPage(),
    'followups' => const DoctorFollowupsPage(),
    _ => const DoctorDashboardPage(),
  };

  String _titleFor(String page) => switch (page) {
    'dashboard' => '工作台',
    'patients' => '患者管理',
    'consultations' => '问诊列表',
    'reports' => '报告审核',
    'followups' => '复查随访',
    _ => '工作台',
  };
}

final doctorPageProvider = NotifierProvider<DoctorPageNotifier, String>(
  DoctorPageNotifier.new,
);

class DoctorPageNotifier extends Notifier<String> {
  @override
  String build() => 'dashboard';
  void set(String page) => state = page;
}
