import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

// pages umum
import 'pages/login_page.dart';
import 'pages/register_page.dart';
import 'pages/landing_page.dart';

// dashboard berdasarkan role
import 'kader/dashboard_kader.dart';
import 'bidan/dashboard_bidan.dart';
import 'ibu/dashboard_ibu.dart';

//route admin
import 'pages/profile_admin.dart';

//route menu kader
import 'kader/data_kehamilan.dart';
import 'kader/data_balita.dart';
import 'kader/data_pemeriksaan.dart';
import 'kader/data_kehadiran.dart';
import 'kader/profile_kader.dart';
import 'kader/data_ibu.dart';

//route menu bidan
import 'bidan/periksa_kehamilan.dart';
import 'bidan/data_gizi_balita.dart';
import 'bidan/imunisasi.dart';
import 'bidan/jadwal_posyandu.dart';
import 'bidan/jadwal_anc.dart';
import 'bidan/edukasi.dart';
import 'bidan/laporan.dart';
import 'bidan/profile_bidan.dart';

//route menu ibu
import 'ibu/pemeriksaan_anc.dart';
import 'ibu/pemeriksaan_balita.dart';
import 'ibu/crud_grafik/grafik_anak_bb_usia.dart';
import 'ibu/crud_grafik/grafik_anak_tb_usia.dart';
import 'ibu/crud_grafik/grafik_anak_bb_tb.dart';
import 'ibu/crud_edukasi/edukasi_balita.dart';
import 'ibu/crud_edukasi/edukasi_ibu_hamil.dart';
import 'ibu/profile_ibu.dart';

//route petugas
import 'pages/petugas.dart';
import 'pages/dashboard_admin.dart';

//route desa
import 'pages/data_desa.dart';

//route dusun
import 'pages/data_dusun.dart';

void main() async {
  // 1. Pastikan inisialisasi Flutter sudah siap sebelum memanggil SharedPreferences
  WidgetsFlutterBinding.ensureInitialized();

  // 2. Akses SharedPreferences untuk mengecek status login
  final prefs = await SharedPreferences.getInstance();

  // 3. Ambil data 'isLogin' dan 'role' (ini harus sesuai dengan kunci yang disimpan di LoginPage)
  bool isLogin = prefs.getBool("isLogin") ?? false;
  String role = prefs.getString("role") ?? "";

  // 4. Tentukan halaman pertama yang muncul saat aplikasi dibuka
  String initialRoute = '/'; // Default ke Landing Page

  if (isLogin) {
    // Jika sudah pernah login, arahkan ke dashboard sesuai role yang tersimpan
    switch (role.toLowerCase()) {
      case "admin":
        initialRoute = '/dashboard-admin';
        break;
      case "kader":
        initialRoute = '/dashboard-kader';
        break;
      case "bidan":
        initialRoute = '/dashboard-bidan';
        break;
      case "ibu":
        initialRoute = '/dashboard-ibu';
        break;
      default:
        initialRoute = '/';
    }
  }

  runApp(MyApp(initialRoute: initialRoute));
}

class MyApp extends StatelessWidget {
  final String initialRoute;
  const MyApp({super.key, required this.initialRoute});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'm-Posyandu', // Nama aplikasi diperbarui

      // Menggunakan rute awal dinamis hasil pengecekan SharedPreferences
      initialRoute: initialRoute,

      routes: {
        // --- PAGES UMUM ---
        '/': (context) => const LandingPage(),
        '/login': (context) {
          final args = ModalRoute.of(context)?.settings.arguments as bool?;
          return LoginPage(fromGuard: args ?? false);
        },
        '/register': (context) => RegisterPage(),

        // --- DASHBOARD BERDASARKAN ROLE ---
        '/dashboard-admin': (context) => const DashboardAdminPage(),
        '/dashboard-kader': (context) => const DashboardKaderPage(),
        '/dashboard-bidan': (context) => const DashboardBidanPage(),
        '/dashboard-ibu': (context) => const DashboardIbuPage(),

        // --- ROUTE ADMIN ---
        '/profile-admin': (context) => const ProfileAdminPage(),
        '/data-petugas': (context) => DataPetugasPage(),
        '/data-desa': (context) => DataDesaPage(),
        '/data-dusun': (context) => DataDusunPage(),

        // --- ROUTE MENU KADER ---
        '/data-ibu': (context) => const DataIbuPage(),
        '/data-kehamilan': (context) => const DataIbuHamilPage(),
        '/data-balita': (context) => const DataBalitaPage(),
        '/data-pemeriksaan-balita': (context) =>
            const DataPemeriksaanBalitaPage(),
        '/data-kehadiran-posyandu': (context) =>
            const DataKehadiranPosyanduPage(),
        '/profile-kader': (context) => const ProfileKaderPage(),

        // --- ROUTE MENU BIDAN ---
        '/periksa-kehamilan': (context) => const DataPemeriksaanKehamilanPage(),
        '/data-gizi-balita': (context) => const DataGiziBalitaPage(),
        '/imunisasi': (context) => const DataImunisasiPage(),
        '/jadwal-posyandu': (context) => const DataJadwalPosyanduPage(),
        '/jadwal-anc': (context) => const JadwalAncPage(),
        '/edukasi': (context) => const DataEdukasiPage(),
        '/profile-bidan': (context) => const ProfileBidanPage(),
        '/laporan': (context) => const DataLaporanPage(),

        // --- ROUTE MENU IBU ---
        '/pemeriksaan-anc': (context) => const PemeriksaanANCPage(),
        '/pemeriksaan-balita': (context) => const PemeriksaanBalitaIbuPage(),
        '/grafik-bb-u': (context) => const GrafikAnakBBUsiaPage(),
        '/grafik-tb-u': (context) => const GrafikAnakTBUPage(),
        '/grafik-bb-tb': (context) => const GrafikAnakBBTBPage(),
        '/edukasi-balita': (context) => const EdukasiBalitaPage(),
        '/edukasi-hamil': (context) => const EdukasiHamilPage(),
        '/profile-ibu': (context) => const ProfileIbuPage(),
      },
    );
  }
}
