import 'package:flutter/material.dart';
import 'package:p_tits_pas/services/configuration_service.dart';
import 'package:p_tits_pas/widgets/admin/assistante_maternelle_management_widget.dart';
import 'package:p_tits_pas/widgets/admin/gestionnaire_management_widget.dart';
import 'package:p_tits_pas/widgets/admin/parent_managmant_widget.dart';
import 'package:p_tits_pas/widgets/admin/admin_management_widget.dart';
import 'package:p_tits_pas/widgets/admin/parametres_panel.dart';
import 'package:p_tits_pas/widgets/app_footer.dart';
import 'package:p_tits_pas/widgets/admin/dashboard_admin.dart';

class AdminDashboardScreen extends StatefulWidget {
  const AdminDashboardScreen({super.key});

  @override
  State<AdminDashboardScreen> createState() => _AdminDashboardScreenState();
}

class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
  bool? _setupCompleted;
  int mainTabIndex = 0;
  int subIndex = 0;

  @override
  void initState() {
    super.initState();
    _loadSetupStatus();
  }

  Future<void> _loadSetupStatus() async {
    try {
      final completed = await ConfigurationService.getSetupStatus();
      if (!mounted) return;
      setState(() {
        _setupCompleted = completed;
        if (!completed) mainTabIndex = 1;
      });
    } catch (e) {
      if (mounted) setState(() {
        _setupCompleted = false;
        mainTabIndex = 1;
      });
    }
  }

  void onMainTabChange(int index) {
    setState(() {
      mainTabIndex = index;
    });
  }

  void onSubTabChange(int index) {
    setState(() {
      subIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_setupCompleted == null) {
      return const Scaffold(
        body: Center(child: CircularProgressIndicator()),
      );
    }
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(60.0),
        child: Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Colors.grey.shade300),
            ),
          ),
          child: DashboardAppBarAdmin(
            selectedIndex: mainTabIndex,
            onTabChange: onMainTabChange,
            setupCompleted: _setupCompleted!,
          ),
        ),
      ),
      body: Column(
        children: [
          if (mainTabIndex == 0)
            DashboardUserManagementSubBar(
              selectedSubIndex: subIndex,
              onSubTabChange: onSubTabChange,
            ),
          Expanded(
            child: _getBody(),
          ),
          const AppFooter(),
        ],
      ),
    );
  }

  Widget _getBody() {
    if (mainTabIndex == 1) {
      return ParametresPanel(redirectToLoginAfterSave: !_setupCompleted!);
    }
    switch (subIndex) {
      case 0:
        return const GestionnaireManagementWidget();
      case 1:
        return const ParentManagementWidget();
      case 2:
        return const AssistanteMaternelleManagementWidget();
      case 3:
        return const AdminManagementWidget();
      default:
        return const Center(child: Text('Page non trouvée'));
    }
  }
}
