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

/// Help page with user guides
class HelpPage extends StatelessWidget {
  const HelpPage({super.key});

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (didPop, result) {
        if (!didPop) {
          context.pop();
        }
      },
      child: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Theme.of(context).colorScheme.primary,
                Theme.of(context).colorScheme.primary.withOpacity(0.9),
                Theme.of(context).colorScheme.secondary,
                Theme.of(context).colorScheme.background,
              ],
              stops: const [0.0, 0.2, 0.6, 1.0],
            ),
          ),
          child: SafeArea(
            top: false,
            child: Column(
              children: [
                // Custom AppBar
                Padding(
                  padding: const EdgeInsets.only(
                    left: 16.0,
                    right: 16.0,
                    bottom: 20.0,
                    top: 52.0,
                  ),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      IconButton(
                        icon: Icon(
                          Icons.arrow_back,
                          color: Theme.of(context).colorScheme.onPrimary,
                        ),
                        onPressed: () => context.pop(),
                      ),
                      Expanded(
                        child: Text(
                          'Ajuda e Suporte',
                          style: TextStyle(
                            color: Theme.of(context).colorScheme.onPrimary,
                            fontSize: 24,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                // Help content
                Expanded(
                  child: SingleChildScrollView(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      children: [
                        _buildGuideCard(
                          context: context,
                          icon: Icons.school,
                          title: 'Como usar o AI Tutor',
                          description:
                              'Aprenda a tirar o máximo partido do seu assistente de IA personalizado.',
                          onTap: () => _showGuide(context, 'AI Tutor'),
                        ),
                        const SizedBox(height: 16),
                        _buildGuideCard(
                          context: context,
                          icon: Icons.quiz,
                          title: 'Como fazer quizzes',
                          description:
                              'Descubra como participar em quizzes interativos e testar os seus conhecimentos.',
                          onTap: () => _showGuide(context, 'Quizzes'),
                        ),
                        const SizedBox(height: 16),
                        _buildGuideCard(
                          context: context,
                          icon: Icons.trending_up,
                          title: 'Ver o seu progresso',
                          description:
                              'Acompanhe a sua evolução de aprendizagem e conquistas.',
                          onTap: () => _showGuide(context, 'Progresso'),
                        ),
                        const SizedBox(height: 16),
                        _buildGuideCard(
                          context: context,
                          icon: Icons.settings,
                          title: 'Configurações da app',
                          description:
                              'Personalize a sua experiência de utilização.',
                          onTap: () => _showGuide(context, 'Configurações'),
                        ),
                        const SizedBox(height: 24),
                        // FAQ Section
                        Container(
                          padding: const EdgeInsets.all(20),
                          decoration: BoxDecoration(
                            color: Theme.of(context).colorScheme.surface,
                            borderRadius: BorderRadius.circular(16),
                            boxShadow: [
                              BoxShadow(
                                color: Theme.of(
                                  context,
                                ).colorScheme.shadow.withOpacity(0.1),
                                blurRadius: 10,
                                offset: const Offset(0, 4),
                              ),
                            ],
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                'Perguntas Frequentes',
                                style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold,
                                  color: Theme.of(
                                    context,
                                  ).colorScheme.onSurface,
                                ),
                              ),
                              const SizedBox(height: 16),
                              _buildFAQItem(
                                context: context,
                                question: 'Como posso alterar a minha senha?',
                                answer:
                                    'Pode alterar a sua senha através da secção de segurança nas definições.',
                              ),
                              const SizedBox(height: 12),
                              _buildFAQItem(
                                context: context,
                                question: 'Os meus dados estão seguros?',
                                answer:
                                    'Sim, utilizamos encriptação de ponta a ponta para proteger todos os seus dados.',
                              ),
                              const SizedBox(height: 12),
                              _buildFAQItem(
                                context: context,
                                question: 'Posso usar a app offline?',
                                answer:
                                    'Algumas funcionalidades estão disponíveis offline, mas para o AI Tutor necessita de conexão à internet.',
                              ),
                              const SizedBox(height: 12),
                              _buildFAQItem(
                                context: context,
                                question: 'Como contactar o suporte?',
                                answer:
                                    'Pode contactar-nos através do email suporte@learnit.com.',
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 24),
                        // Contact Support
                        Container(
                          padding: const EdgeInsets.all(20),
                          decoration: BoxDecoration(
                            color: Theme.of(context).colorScheme.surface,
                            borderRadius: BorderRadius.circular(16),
                            boxShadow: [
                              BoxShadow(
                                color: Theme.of(
                                  context,
                                ).colorScheme.shadow.withOpacity(0.1),
                                blurRadius: 10,
                                offset: const Offset(0, 4),
                              ),
                            ],
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                'Ainda precisa de ajuda?',
                                style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold,
                                  color: Theme.of(
                                    context,
                                  ).colorScheme.onSurface,
                                ),
                              ),
                              const SizedBox(height: 8),
                              Text(
                                'A nossa equipa de suporte está disponível para ajudar.',
                                style: TextStyle(
                                  fontSize: 14,
                                  color: Theme.of(
                                    context,
                                  ).colorScheme.onSurfaceVariant,
                                ),
                              ),
                              const SizedBox(height: 16),
                              ElevatedButton.icon(
                                onPressed: () {
                                  // Open email client
                                },
                                icon: const Icon(Icons.email),
                                label: const Text('Contactar Suporte'),
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: Theme.of(
                                    context,
                                  ).colorScheme.primary,
                                  foregroundColor: Theme.of(
                                    context,
                                  ).colorScheme.onPrimary,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildGuideCard({
    required BuildContext context,
    required IconData icon,
    required String title,
    required String description,
    required VoidCallback onTap,
  }) {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surface,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Theme.of(context).colorScheme.shadow.withOpacity(0.1),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(16),
        child: Row(
          children: [
            Container(
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    Theme.of(context).colorScheme.primary,
                    Theme.of(context).colorScheme.primary.withOpacity(0.8),
                  ],
                ),
                borderRadius: BorderRadius.circular(25),
              ),
              child: Icon(
                icon,
                color: Theme.of(context).colorScheme.onPrimary,
                size: 24,
              ),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Theme.of(context).colorScheme.onSurface,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    description,
                    style: TextStyle(
                      fontSize: 12,
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                    ),
                  ),
                ],
              ),
            ),
            Icon(
              Icons.chevron_right,
              color: Theme.of(context).colorScheme.onSurfaceVariant,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFAQItem({
    required BuildContext context,
    required String question,
    required String answer,
  }) {
    return ExpansionTile(
      title: Text(
        question,
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w600,
          color: Theme.of(context).colorScheme.onSurface,
        ),
      ),
      children: [
        Padding(
          padding: const EdgeInsets.all(16.0),
          child: Text(
            answer,
            style: TextStyle(
              fontSize: 13,
              color: Theme.of(context).colorScheme.onSurfaceVariant,
            ),
          ),
        ),
      ],
    );
  }

  void _showGuide(BuildContext context, String guideTitle) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(guideTitle),
        content: const Text('Conteúdo do guia em desenvolvimento...'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Fechar'),
          ),
        ],
      ),
    );
  }
}
