import 'package:flutter/material.dart';
import 'package:p_tits_pas/controllers/parent_dashboard_controller.dart';
import 'package:p_tits_pas/services/dashboardService.dart';
import 'package:p_tits_pas/widgets/app_footer.dart';
import 'package:p_tits_pas/widgets/dashbord_parent/app_layout.dart';
import 'package:p_tits_pas/widgets/dashbord_parent/children_sidebar.dart';
import 'package:p_tits_pas/widgets/dashbord_parent/dashboard_app_bar.dart';
import 'package:p_tits_pas/widgets/dashbord_parent/wid_dashbord.dart';
import 'package:p_tits_pas/widgets/main_content_area.dart';
import 'package:p_tits_pas/widgets/messaging_sidebar.dart';
import 'package:provider/provider.dart';

class ParentDashboardScreen extends StatefulWidget {
  const ParentDashboardScreen({Key? key}) : super(key: key);

  @override
  State<ParentDashboardScreen> createState() => _ParentDashboardScreenState();
}

class _ParentDashboardScreenState extends State<ParentDashboardScreen> {
  int selectedIndex = 0;

  void onTabChange(int index) {
    setState(() {
      selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    // Initialiser les données du dashboard
    WidgetsBinding.instance.addPostFrameCallback((_) {
      context.read<ParentDashboardController>().initDashboard();
    });
  }

  Widget _getBody() {
    switch (selectedIndex) {
      case 0:
        return Dashbord_body();
      case 1:
        return const Center(child: Text("🔍 Trouver une nounou"));
      case 2:
        return const Center(child: Text("⚙️ Paramètres"));
      default:
        return const Center(child: Text("Page non trouvée"));
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => ParentDashboardController(DashboardService())..initDashboard(),
      child: Scaffold(
        appBar: PreferredSize(preferredSize: const Size.fromHeight(60.0),
        child: Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Colors.grey.shade300),
            ),
          ),
          child: DashboardAppBar(
            selectedIndex: selectedIndex,
            onTabChange: onTabChange,
          ),
        ),
      ),
      body: Column(
        children: [
          Expanded (child: _getBody(),
          ),
          const AppFooter(),
        ],
      ),
      )
      // body: _buildResponsiveBody(context, controller),
      //  footer: const AppFooter(),
    );
  }

  Widget _buildResponsiveBody(BuildContext context, ParentDashboardController controller) {
    return LayoutBuilder(
      builder: (context, constraints) {
        if (constraints.maxWidth < 768) {
          // Layout mobile : colonnes empilées
          return _buildMobileLayout(controller);
        } else if (constraints.maxWidth < 1024) {
          // Layout tablette : 2 colonnes
          return _buildTabletLayout(controller);
        } else {
          // Layout desktop : 3 colonnes
          return _buildDesktopLayout(controller);
        }
      },
    );
  }

  Widget _buildDesktopLayout(ParentDashboardController controller) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // Sidebar gauche - Enfants
        SizedBox(
          width: 280,
          child: ChildrenSidebar(
            children: controller.children,
            selectedChildId: controller.selectedChildId,
            onChildSelected: controller.selectChild,
            onAddChild: controller.showAddChildModal,
          ),
        ),
        
        // Contenu central
        Expanded(
          flex: 2,
          child: MainContentArea(
            selectedChild: controller.selectedChild,
            selectedAssistant: controller.selectedAssistant,
            events: controller.upcomingEvents,
            contracts: controller.contracts,
          ),
        ),
        
        // Sidebar droite - Messagerie
        SizedBox(
          width: 320,
          child: MessagingSidebar(
            conversations: controller.conversations,
            notifications: controller.notifications,
          ),
        ),
      ],
    );
  }

  Widget _buildTabletLayout(ParentDashboardController controller) {
    return Row(
      children: [
        // Sidebar enfants plus étroite
        SizedBox(
          width: 240,
          child: ChildrenSidebar(
            children: controller.children,
            selectedChildId: controller.selectedChildId,
            onChildSelected: controller.selectChild,
            onAddChild: controller.showAddChildModal,
            isCompact: true,
          ),
        ),
        
        // Contenu principal avec messagerie intégrée
        Expanded(
          child: Column(
            children: [
              Expanded(
                flex: 2,
                child: MainContentArea(
                  selectedChild: controller.selectedChild,
                  selectedAssistant: controller.selectedAssistant,
                  events: controller.upcomingEvents,
                  contracts: controller.contracts,
                ),
              ),
              SizedBox(
                height: 200,
                child: MessagingSidebar(
                  conversations: controller.conversations,
                  notifications: controller.notifications,
                  isCompact: true,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildMobileLayout(ParentDashboardController controller) {
    return DefaultTabController(
      length: 4,
      child: Column(
        children: [
          // Navigation par onglets sur mobile
          Container(
            color: Theme.of(context).primaryColor.withOpacity(0.1),
            child: const TabBar(
              isScrollable: true,
              tabs: [
                Tab(text: 'Enfants', icon: Icon(Icons.child_care)),
                Tab(text: 'Planning', icon: Icon(Icons.calendar_month)),
                Tab(text: 'Contrats', icon: Icon(Icons.description)),
                Tab(text: 'Messages', icon: Icon(Icons.message)),
              ],
            ),
          ),
          
          Expanded(
            child: TabBarView(
              children: [
                // Onglet Enfants
                ChildrenSidebar(
                  children: controller.children,
                  selectedChildId: controller.selectedChildId,
                  onChildSelected: controller.selectChild,
                  onAddChild: controller.showAddChildModal,
                  isMobile: true,
                ),
                
                // Onglet Planning
                MainContentArea(
                  selectedChild: controller.selectedChild,
                  selectedAssistant: controller.selectedAssistant,
                  events: controller.upcomingEvents,
                  contracts: controller.contracts,
                  showOnlyCalendar: true,
                ),
                
                // Onglet Contrats
                MainContentArea(
                  selectedChild: controller.selectedChild,
                  selectedAssistant: controller.selectedAssistant,
                  events: controller.upcomingEvents,
                  contracts: controller.contracts,
                  showOnlyContracts: true,
                ),
                
                // Onglet Messages
                MessagingSidebar(
                  conversations: controller.conversations,
                  notifications: controller.notifications,
                  isMobile: true,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}