// lib/features/admin/tabs/staff_lectures/staff_lectures_main_tab.dart

import 'package:flutter/material.dart';

import 'active_lectures_sub_tab.dart';
import 'past_lectures_sub_tab.dart';

const Color _standardGold = Color.fromARGB(255, 204, 117, 4);
const Color _darkGreen = Color.fromARGB(255, 2, 105, 48);
const Color _ink = Color(0xFF24332B);

class LectureHoverClickable extends StatefulWidget {
  final Widget Function(bool isHovered) builder;
  final VoidCallback? onTap;

  const LectureHoverClickable({super.key, required this.builder, this.onTap});

  @override
  State<LectureHoverClickable> createState() => _LectureHoverClickableState();
}

class _LectureHoverClickableState extends State<LectureHoverClickable> {
  bool _isHovered = false;

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onEnter: (_) => setState(() => _isHovered = true),
      onExit: (_) => setState(() => _isHovered = false),
      cursor: widget.onTap != null ? SystemMouseCursors.click : SystemMouseCursors.basic,
      child: GestureDetector(onTap: widget.onTap, child: widget.builder(_isHovered)),
    );
  }
}

class LectureHoverMenuLink extends StatefulWidget {
  final String title;
  final String subtitle;
  final IconData icon;
  final bool isSelected;
  final VoidCallback onTap;

  const LectureHoverMenuLink({
    super.key,
    required this.title,
    required this.subtitle,
    required this.icon,
    required this.isSelected,
    required this.onTap,
  });

  @override
  State<LectureHoverMenuLink> createState() => _LectureHoverMenuLinkState();
}

class _LectureHoverMenuLinkState extends State<LectureHoverMenuLink> {
  bool _isHovered = false;

  @override
  Widget build(BuildContext context) {
    final active = widget.isSelected || _isHovered;
    return MouseRegion(
      onEnter: (_) => setState(() => _isHovered = true),
      onExit: (_) => setState(() => _isHovered = false),
      cursor: SystemMouseCursors.click,
      child: GestureDetector(
        onTap: widget.onTap,
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 160),
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
          decoration: BoxDecoration(
            color: widget.isSelected
                ? _darkGreen.withOpacity(0.08)
                : (_isHovered ? const Color(0xFFF7F9F8) : Colors.transparent),
            border: Border(
              left: BorderSide(color: active ? _standardGold : Colors.transparent, width: 4),
            ),
          ),
          child: Row(
            children: [
              Container(
                width: 42,
                height: 42,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: widget.isSelected ? _darkGreen : const Color(0xFFF1F5F2),
                ),
                child: Icon(
                  widget.icon,
                  color: widget.isSelected ? Colors.white : _darkGreen,
                  size: 22,
                ),
              ),
              const SizedBox(width: 14),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      widget.title,
                      style: TextStyle(
                        fontSize: 13.5,
                        fontWeight: FontWeight.w800,
                        color: widget.isSelected ? _darkGreen : _ink,
                        letterSpacing: 0.35,
                      ),
                    ),
                    const SizedBox(height: 3),
                    Text(
                      widget.subtitle,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(color: Color(0xFF839088), fontSize: 10.5),
                    ),
                  ],
                ),
              ),
              Icon(
                Icons.chevron_right_rounded,
                color: active ? _standardGold : const Color(0xFFB2BBB5),
                size: 20,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

  @override
  State<StaffLecturesMainTab> createState() => _StaffLecturesMainTabState();
}

class _StaffLecturesMainTabState extends State<StaffLecturesMainTab> {
  int _currentViewIndex = 0;
  bool _showHeader = true;
  bool _isSidebarOpen = false;

  final List<_MenuEntry> _menuItems = const [
    _MenuEntry(
      title: 'Teaching Staff',
      subtitle: 'Staff profiles and campus allocation',
      icon: Icons.groups_2_outlined,
    ),
    _MenuEntry(
      title: 'Report Lecturer',
      subtitle: 'Submit a staff conduct report',
      icon: Icons.report_gmailerrorred_outlined,
    ),
  ];

  void _switchTab(int index) {
    setState(() {
      _currentViewIndex = index;
      _isSidebarOpen = false;
    });
  }

  void _toggleSidebar() => setState(() => _isSidebarOpen = !_isSidebarOpen);

  Widget _getCurrentTab() {
    switch (_currentViewIndex) {
      case 0:
        return const ActiveLecturesSubTab(key: ValueKey('staff-directory'));
      case 1:
        return const PastLecturesSubTab(key: ValueKey('report-lecturer'));
      default:
        return const ActiveLecturesSubTab(key: ValueKey('staff-directory'));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF4F6F5),
      body: LayoutBuilder(
        builder: (context, constraints) {
          final isDesktop = constraints.maxWidth > 800;

          return Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  AnimatedSize(
                    duration: const Duration(milliseconds: 350),
                    curve: Curves.easeInOutCubic,
                    alignment: Alignment.topCenter,
                    clipBehavior: Clip.hardEdge,
                    child: _showHeader
                        ? _buildHeader(isDesktop)
                        : const SizedBox(width: double.infinity, height: 0),
                  ),
                  if (!_showHeader) const SizedBox(height: 48),
                  Expanded(
                    child: AnimatedSwitcher(
                      duration: const Duration(milliseconds: 280),
                      switchInCurve: Curves.easeOutCubic,
                      switchOutCurve: Curves.easeInCubic,
                      transitionBuilder: (child, animation) {
                        final slide = Tween<Offset>(
                          begin: const Offset(0.02, 0),
                          end: Offset.zero,
                        ).animate(animation);
                        return FadeTransition(
                          opacity: animation,
                          child: SlideTransition(position: slide, child: child),
                        );
                      },
                      child: _getCurrentTab(),
                    ),
                  ),
                ],
              ),

              if (!_showHeader)
                Positioned(top: 18, left: 18, child: _buildFloatingHamburger()),
              if (!_showHeader)
                Positioned(
                  top: 0,
                  left: 0,
                  right: 0,
                  child: Center(child: _buildChevron(onTap: () => setState(() => _showHeader = true))),
                ),

              if (_isSidebarOpen)
                Positioned.fill(
                  child: GestureDetector(
                    onTap: _toggleSidebar,
                    child: Container(color: Colors.black.withOpacity(0.30)),
                  ),
                ),

              AnimatedPositioned(
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeOutCubic,
                top: 0,
                bottom: 0,
                left: _isSidebarOpen ? 0 : -330,
                child: _buildSidebar(),
              ),
            ],
          );
        },
      ),
    );
  }

  Widget _buildHeader(bool isDesktop) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        border: Border(bottom: BorderSide(color: Color(0xFFDCE3DE))),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(height: 4, color: _standardGold),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: isDesktop ? 28 : 16, vertical: 18),
            child: Row(
              children: [
                LectureHoverClickable(
                  onTap: _toggleSidebar,
                  builder: (hovered) => AnimatedContainer(
                    duration: const Duration(milliseconds: 160),
                    width: 46,
                    height: 46,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: hovered ? const Color(0xFFF1F5F2) : Colors.white,
                      border: Border.all(color: const Color(0xFFD7DFDA)),
                    ),
                    child: const Icon(Icons.menu_rounded, color: _darkGreen, size: 25),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      RichText(
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        text: TextSpan(
                          children: [
                            TextSpan(
                              text: 'ROCKVIEW ',
                              style: TextStyle(
                                color: _darkGreen,
                                fontSize: isDesktop ? 22 : 17,
                                fontWeight: FontWeight.w900,
                                letterSpacing: 1,
                              ),
                            ),
                            TextSpan(
                              text: 'STAFF MANAGEMENT',
                              style: TextStyle(
                                color: _standardGold,
                                fontSize: isDesktop ? 22 : 17,
                                fontWeight: FontWeight.w500,
                                letterSpacing: 1,
                              ),
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(height: 5),
                      Row(
                        children: [
                          Container(
                            width: 7,
                            height: 7,
                            decoration: const BoxDecoration(color: _darkGreen, shape: BoxShape.circle),
                          ),
                          const SizedBox(width: 7),
                          Flexible(
                            child: Text(
                              'Staff directory and lecturer reporting synchronized with university records',
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: isDesktop ? 12 : 10.5,
                                color: const Color(0xFF738178),
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                if (isDesktop) ...[
                  Container(
                    padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                    decoration: const BoxDecoration(color: Color(0xFFF6F8F7)),
                    child: Row(
                      children: [
                        const Icon(Icons.shield_outlined, size: 17, color: _darkGreen),
                        const SizedBox(width: 7),
                        Text(
                          'ADMIN VIEW',
                          style: TextStyle(
                            color: Colors.grey.shade700,
                            fontWeight: FontWeight.w800,
                            fontSize: 10.5,
                            letterSpacing: 0.8,
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(width: 12),
                  _buildSharpCollapseButton(onTap: () => setState(() => _showHeader = false)),
                ],
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSidebar() {
    return Container(
      width: 330,
      decoration: const BoxDecoration(
        color: Colors.white,
        border: Border(right: BorderSide(color: Color(0xFFDCE3DE))),
        boxShadow: [BoxShadow(color: Color(0x24000000), blurRadius: 26, offset: Offset(5, 0))],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            padding: const EdgeInsets.fromLTRB(24, 26, 16, 22),
            decoration: const BoxDecoration(
              color: _darkGreen,
              border: Border(bottom: BorderSide(color: _standardGold, width: 4)),
            ),
            child: Row(
              children: [
                Container(
                  width: 42,
                  height: 42,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Colors.white.withOpacity(0.10),
                    border: Border.all(color: Colors.white.withOpacity(0.20)),
                  ),
                  child: const Icon(Icons.cast_for_education_outlined, color: Colors.white),
                ),
                const SizedBox(width: 13),
                const Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'STAFF MANAGEMENT',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 15,
                          fontWeight: FontWeight.w900,
                          letterSpacing: 1.2,
                        ),
                      ),
                      SizedBox(height: 3),
                      Text('Personnel directory & reporting', style: TextStyle(color: Color(0xFFBFD8C8), fontSize: 10.5)),
                    ],
                  ),
                ),
                IconButton(
                  onPressed: _toggleSidebar,
                  icon: const Icon(Icons.close_rounded, color: Colors.white),
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(20, 22, 20, 10),
            child: Text(
              'NAVIGATION',
              style: TextStyle(
                color: Colors.grey.shade500,
                fontSize: 10,
                fontWeight: FontWeight.w900,
                letterSpacing: 1.2,
              ),
            ),
          ),
          Expanded(
            child: ListView.builder(
              padding: const EdgeInsets.only(top: 4),
              itemCount: _menuItems.length,
              itemBuilder: (context, index) {
                final item = _menuItems[index];
                return LectureHoverMenuLink(
                  title: item.title,
                  subtitle: item.subtitle,
                  icon: item.icon,
                  isSelected: _currentViewIndex == index,
                  onTap: () => _switchTab(index),
                );
              },
            ),
          ),
          Container(
            padding: const EdgeInsets.all(20),
            decoration: const BoxDecoration(
              color: Color(0xFFF8FAF8),
              border: Border(top: BorderSide(color: Color(0xFFE1E8E3))),
            ),
            child: const Row(
              children: [
                Icon(Icons.account_balance_outlined, color: _standardGold, size: 20),
                SizedBox(width: 10),
                Expanded(
                  child: Text(
                    'Rockview University\nStaff Management System',
                    style: TextStyle(color: Color(0xFF718078), fontSize: 10.5, height: 1.45),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFloatingHamburger() {
    return LectureHoverClickable(
      onTap: _toggleSidebar,
      builder: (hovered) => AnimatedContainer(
        duration: const Duration(milliseconds: 160),
        width: 46,
        height: 46,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: hovered ? const Color(0xFFF1F5F2) : Colors.white,
          border: Border.all(color: const Color(0xFFD7DFDA)),
          boxShadow: const [BoxShadow(color: Color(0x13000000), blurRadius: 8, offset: Offset(0, 3))],
        ),
        child: const Icon(Icons.menu_rounded, color: _darkGreen, size: 24),
      ),
    );
  }

  Widget _buildSharpCollapseButton({required VoidCallback onTap}) {
    return LectureHoverClickable(
      onTap: onTap,
      builder: (hovered) => AnimatedContainer(
        duration: const Duration(milliseconds: 160),
        width: 42,
        height: 42,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: hovered ? _standardGold : Colors.white,
          border: Border.all(color: _standardGold, width: 1.5),
        ),
        child: Icon(
          Icons.keyboard_arrow_up_rounded,
          color: hovered ? Colors.white : _standardGold,
          size: 24,
        ),
      ),
    );
  }

  Widget _buildChevron({required VoidCallback onTap}) {
    return LectureHoverClickable(
      onTap: onTap,
      builder: (hovered) => AnimatedContainer(
        duration: const Duration(milliseconds: 160),
        padding: const EdgeInsets.symmetric(horizontal: 22, vertical: 7),
        decoration: BoxDecoration(
          color: hovered ? _standardGold : Colors.white,
          border: Border.all(color: _standardGold, width: 1.5),
          boxShadow: const [BoxShadow(color: Color(0x13000000), blurRadius: 8, offset: Offset(0, 3))],
        ),
        child: Icon(
          Icons.keyboard_arrow_down_rounded,
          color: hovered ? Colors.white : _darkGreen,
          size: 23,
        ),
      ),
    );
  }
}

class _MenuEntry {
  final String title;
  final String subtitle;
  final IconData icon;

  const _MenuEntry({required this.title, required this.subtitle, required this.icon});
}
