/// Dashboard Consultant - Interface Limitée
/// Interface spécialisée pour consultants externes
library consultant_dashboard;

import 'package:flutter/material.dart';

/// Dashboard pour Consultant Externe
class ConsultantDashboard extends StatefulWidget {
  const ConsultantDashboard({super.key});

  @override
  State<ConsultantDashboard> createState() => _ConsultantDashboardState();
}

class _ConsultantDashboardState extends State<ConsultantDashboard> {
  int _selectedIndex = 0;
  
  final List<String> _consultantSections = [
    'Mes Projets',
    'Contacts',
    'Profil',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF8F9FA),
      appBar: AppBar(
        title: Text(
          'Consultant - ${_consultantSections[_selectedIndex]}',
          style: const TextStyle(
            color: Color(0xFF6C5CE7),
            fontWeight: FontWeight.bold,
            fontSize: 18,
          ),
        ),
        backgroundColor: Colors.white,
        elevation: 2,
        centerTitle: false,
        actions: [
          // Notifications consultant
          IconButton(
            icon: const Icon(Icons.notifications_outlined, color: Color(0xFF6C5CE7)),
            onPressed: () => _showConsultantNotifications(),
            tooltip: 'Mes notifications',
          ),
          // Menu consultant
          PopupMenuButton<String>(
            icon: const Icon(Icons.more_vert, color: Color(0xFF6C5CE7)),
            onSelected: (value) {
              switch (value) {
                case 'profile':
                  _editProfile();
                  break;
                case 'contact':
                  _contactSupport();
                  break;
                case 'help':
                  _showHelp();
                  break;
              }
            },
            itemBuilder: (context) => [
              const PopupMenuItem(
                value: 'profile',
                child: Row(
                  children: [
                    Icon(Icons.person, size: 20, color: Color(0xFF6C5CE7)),
                    SizedBox(width: 12),
                    Text('Mon Profil'),
                  ],
                ),
              ),
              const PopupMenuItem(
                value: 'contact',
                child: Row(
                  children: [
                    Icon(Icons.support_agent, size: 20, color: Color(0xFF6C5CE7)),
                    SizedBox(width: 12),
                    Text('Support'),
                  ],
                ),
              ),
              const PopupMenuItem(
                value: 'help',
                child: Row(
                  children: [
                    Icon(Icons.help, size: 20, color: Color(0xFF6C5CE7)),
                    SizedBox(width: 12),
                    Text('Aide'),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
      drawer: _buildConsultantDrawer(),
      body: Stack(
        children: [
          _buildSelectedContent(),
          // Navigation rapide consultant
          Positioned(
            bottom: 20,
            left: 20,
            right: 20,
            child: _buildConsultantQuickNavigation(),
          ),
        ],
      ),
    );
  }

  /// Drawer de navigation consultant
  Widget _buildConsultantDrawer() {
    return Drawer(
      child: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Color(0xFF6C5CE7),
              Color(0xFF5A4FCF),
              Color(0xFF4834D4),
            ],
          ),
        ),
        child: Column(
          children: [
            // Header consultant
            Container(
              padding: const EdgeInsets.fromLTRB(20, 60, 20, 20),
              child: Row(
                children: [
                  Container(
                    width: 60,
                    height: 60,
                    decoration: BoxDecoration(
                      color: Colors.white.withOpacity(0.2),
                      borderRadius: BorderRadius.circular(30),
                      border: Border.all(
                        color: Colors.white.withOpacity(0.3),
                        width: 2,
                      ),
                    ),
                    child: const Icon(
                      Icons.business_center,
                      color: Colors.white,
                      size: 30,
                    ),
                  ),
                  const SizedBox(width: 16),
                  const Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Sophie Martin',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Text(
                          'Consultant IT',
                          style: TextStyle(
                            color: Colors.white70,
                            fontSize: 14,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            
            // Menu de navigation
            Expanded(
              child: ListView.builder(
                padding: const EdgeInsets.symmetric(horizontal: 8),
                itemCount: _consultantSections.length,
                itemBuilder: (context, index) {
                  final isSelected = _selectedIndex == index;
                  return Container(
                    margin: const EdgeInsets.symmetric(vertical: 2),
                    decoration: BoxDecoration(
                      color: isSelected 
                          ? Colors.white.withOpacity(0.2)
                          : Colors.transparent,
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: ListTile(
                      leading: Icon(
                        _getConsultantSectionIcon(index),
                        color: Colors.white,
                        size: 22,
                      ),
                      title: Text(
                        _consultantSections[index],
                        style: TextStyle(
                          color: Colors.white,
                          fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
                        ),
                      ),
                      onTap: () {
                        setState(() => _selectedIndex = index);
                        Navigator.pop(context);
                      },
                    ),
                  );
                },
              ),
            ),
            
            // Footer avec déconnexion
            Container(
              padding: const EdgeInsets.all(16),
              child: ElevatedButton.icon(
                onPressed: () {
                  Navigator.of(context).pop();
                  // TODO: Implémenter la déconnexion
                },
                icon: const Icon(Icons.logout, size: 16),
                label: const Text('Déconnexion'),
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.white.withOpacity(0.2),
                  foregroundColor: Colors.white,
                  minimumSize: const Size(double.infinity, 40),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// Icône pour chaque section consultant
  IconData _getConsultantSectionIcon(int index) {
    switch (index) {
      case 0: return Icons.work;
      case 1: return Icons.contacts;
      case 2: return Icons.person;
      default: return Icons.work;
    }
  }

  /// Contenu de la section sélectionnée
  Widget _buildSelectedContent() {
    switch (_selectedIndex) {
      case 0:
        return _buildProjectsContent();
      case 1:
        return _buildContactsContent();
      case 2:
        return _buildProfileContent();
      default:
        return _buildProjectsContent();
    }
  }

  /// Mes Projets - Vue des projets assignés
  Widget _buildProjectsContent() {
    return SingleChildScrollView(
      padding: const EdgeInsets.fromLTRB(16, 16, 16, 80),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header projets
          _buildProjectsHeader(),
          const SizedBox(height: 20),
          
          // Projets actifs
          _buildActiveProjects(),
          const SizedBox(height: 20),
          
          // Tâches en cours
          _buildCurrentTasks(),
          const SizedBox(height: 20),
          
          // Statistiques consultant
          _buildConsultantStats(),
        ],
      ),
    );
  }

  /// Placeholder pour les autres sections
  Widget _buildContactsContent() {
    return const Center(
      child: Text(
        'Contacts\n(En développement)',
        textAlign: TextAlign.center,
        style: TextStyle(fontSize: 18),
      ),
    );
  }

  Widget _buildProfileContent() {
    return const Center(
      child: Text(
        'Mon Profil\n(En développement)',
        textAlign: TextAlign.center,
        style: TextStyle(fontSize: 18),
      ),
    );
  }

  /// Header projets
  Widget _buildProjectsHeader() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: const Row(
        children: [
          Icon(Icons.work, color: Color(0xFF6C5CE7), size: 24),
          SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Mes Projets Assignés',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  '3 projets actifs',
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 14,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// Projets actifs
  Widget _buildActiveProjects() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Projets Actifs',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        _buildProjectCard(
          'Refonte Site Web',
          'Développement frontend',
          '75%',
          const Color(0xFF00B894),
        ),
        const SizedBox(height: 8),
        _buildProjectCard(
          'App Mobile',
          'Interface utilisateur',
          '45%',
          const Color(0xFF0984E3),
        ),
        const SizedBox(height: 8),
        _buildProjectCard(
          'API Backend',
          'Architecture serveur',
          '90%',
          const Color(0xFFE17055),
        ),
      ],
    );
  }

  /// Widget pour une carte de projet
  Widget _buildProjectCard(String title, String description, String progress, Color color) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: color.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(Icons.folder, color: color, size: 20),
              ),
              const SizedBox(width: 12),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: const TextStyle(
                        fontWeight: FontWeight.w600,
                        fontSize: 16,
                      ),
                    ),
                    Text(
                      description,
                      style: TextStyle(
                        color: Colors.grey[600],
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
              ),
              Text(
                progress,
                style: TextStyle(
                  color: color,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),
          LinearProgressIndicator(
            value: double.parse(progress.replaceAll('%', '')) / 100,
            backgroundColor: color.withOpacity(0.2),
            valueColor: AlwaysStoppedAnimation<Color>(color),
          ),
        ],
      ),
    );
  }

  /// Tâches en cours
  Widget _buildCurrentTasks() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Tâches du Jour',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.05),
                blurRadius: 10,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Column(
            children: [
              _buildTaskItem('Révision code frontend', true),
              const SizedBox(height: 8),
              _buildTaskItem('Réunion client 15h', false),
              const SizedBox(height: 8),
              _buildTaskItem('Tests unitaires', false),
            ],
          ),
        ),
      ],
    );
  }

  /// Widget pour un élément de tâche
  Widget _buildTaskItem(String task, bool completed) {
    return Row(
      children: [
        Container(
          width: 20,
          height: 20,
          decoration: BoxDecoration(
            color: completed ? const Color(0xFF6C5CE7) : Colors.transparent,
            border: Border.all(color: const Color(0xFF6C5CE7), width: 2),
            borderRadius: BorderRadius.circular(4),
          ),
          child: completed
              ? const Icon(Icons.check, color: Colors.white, size: 14)
              : null,
        ),
        const SizedBox(width: 12),
        Expanded(
          child: Text(
            task,
            style: TextStyle(
              fontSize: 14,
              decoration: completed ? TextDecoration.lineThrough : null,
              color: completed ? Colors.grey[600] : Colors.black,
            ),
          ),
        ),
      ],
    );
  }

  /// Statistiques consultant
  Widget _buildConsultantStats() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Mes Statistiques',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 12),
        Row(
          children: [
            Expanded(
              child: _buildStatCard('Projets', '3', Icons.work, const Color(0xFF6C5CE7)),
            ),
            const SizedBox(width: 12),
            Expanded(
              child: _buildStatCard('Tâches', '12', Icons.task, const Color(0xFF00B894)),
            ),
          ],
        ),
        const SizedBox(height: 12),
        Row(
          children: [
            Expanded(
              child: _buildStatCard('Heures', '156h', Icons.schedule, const Color(0xFF0984E3)),
            ),
            const SizedBox(width: 12),
            Expanded(
              child: _buildStatCard('Éval.', '4.8/5', Icons.star, const Color(0xFFFDAB00)),
            ),
          ],
        ),
      ],
    );
  }

  /// Widget pour une carte de statistique
  Widget _buildStatCard(String title, String value, IconData icon, Color color) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: color.withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Icon(icon, color: color, size: 24),
          ),
          const SizedBox(height: 8),
          Text(
            value,
            style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          Text(
            title,
            style: TextStyle(
              color: Colors.grey[600],
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }

  /// Navigation rapide consultant
  Widget _buildConsultantQuickNavigation() {
    return Container(
      height: 60,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(30),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 20,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          _buildNavItem(Icons.work, 'Projets', 0),
          _buildNavItem(Icons.contacts, 'Contacts', 1),
          _buildNavItem(Icons.person, 'Profil', 2),
        ],
      ),
    );
  }

  /// Widget pour un élément de navigation
  Widget _buildNavItem(IconData icon, String label, int index) {
    final isSelected = _selectedIndex == index;
    return GestureDetector(
      onTap: () => setState(() => _selectedIndex = index),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color: isSelected
                    ? const Color(0xFF6C5CE7).withOpacity(0.1)
                    : Colors.transparent,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Icon(
                icon,
                size: 18,
                color: isSelected
                    ? const Color(0xFF6C5CE7)
                    : Colors.grey[600],
              ),
            ),
            const SizedBox(height: 2),
            Text(
              label,
              style: TextStyle(
                fontSize: 9,
                fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
                color: isSelected
                    ? const Color(0xFF6C5CE7)
                    : Colors.grey[600],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // Méthodes d'action
  void _showConsultantNotifications() {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Notifications consultant - Fonctionnalité à implémenter'),
        backgroundColor: Color(0xFF6C5CE7),
      ),
    );
  }

  void _editProfile() {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Éditer profil - Fonctionnalité à implémenter'),
        backgroundColor: Color(0xFF6C5CE7),
      ),
    );
  }

  void _contactSupport() {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Contacter support - Fonctionnalité à implémenter'),
        backgroundColor: Color(0xFF6C5CE7),
      ),
    );
  }

  void _showHelp() {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Aide - Fonctionnalité à implémenter'),
        backgroundColor: Color(0xFF6C5CE7),
      ),
    );
  }
}
