import 'dart:math';

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

import '../../../../core/services/auth_service.dart';
import '../../../../features/materials/presentation/pages/teacher_materials_page.dart';
import 'dashboard_action_card.dart';

/// Quick access cards for teacher actions
class TeacherQuickActionsWidget extends StatefulWidget {
  const TeacherQuickActionsWidget({super.key});

  @override
  State<TeacherQuickActionsWidget> createState() =>
      _TeacherQuickActionsWidgetState();
}

class _TeacherQuickActionsWidgetState extends State<TeacherQuickActionsWidget> {
  bool _isCreatingClass = false;

  /// Mesmas dimensões dos cards em "As Minhas Turmas".
  static const double _scrollCardWidth = 200;
  static const double _scrollRowHeight = 156;
  static const double _cardMinHeight = 156;
  static const EdgeInsets _cardPadding = EdgeInsets.all(16);
  static const double _titleFontSize = 16;
  static const double _subtitleFontSize = 13;
  static const double _iconSize = 24;
  static const double _iconPadding = 10;

  @override
  Widget build(BuildContext context) {
    final cards = [
      _buildUploadContentCard(context),
      _buildCreateClassCard(context),
      _buildCreateQuizCard(context),
      _buildViewAnalyticsCard(context),
    ];

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        InkWell(
          onTap: () => _showQuickActionsList(context),
          borderRadius: BorderRadius.circular(8),
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  'Ações Rápidas',
                  style: TextStyle(
                    color: Theme.of(context).colorScheme.onSurface,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(width: 8),
                Icon(
                  Icons.expand_more,
                  color: Theme.of(context).colorScheme.onSurfaceVariant,
                  size: 22,
                ),
              ],
            ),
          ),
        ),
        const SizedBox(height: 12),
        SizedBox(
          height: _scrollRowHeight,
          child: ListView.separated(
            scrollDirection: Axis.horizontal,
            clipBehavior: Clip.none,
            padding: const EdgeInsets.only(right: 16),
            itemCount: cards.length,
            separatorBuilder: (_, __) => const SizedBox(width: 12),
            itemBuilder: (context, index) =>
                SizedBox(width: _scrollCardWidth, child: cards[index]),
          ),
        ),
      ],
    );
  }

  Widget _buildUploadContentCard(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(16),
      child: DashboardActionCard(
        title: 'Upload Conteúdo',
        subtitle: 'PDFs, textos, imagens',
        icon: Icons.upload_file,
        useGradient: true,
        minHeight: _cardMinHeight,
        iconSize: _iconSize,
        iconPadding: _iconPadding,
        titleFontSize: _titleFontSize,
        subtitleFontSize: _subtitleFontSize,
        padding: _cardPadding,
        onTap: () => Navigator.push(
          context,
          MaterialPageRoute(builder: (_) => const TeacherMaterialsPage()),
        ),
      ),
    );
  }

  Widget _buildCreateQuizCard(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(16),
      child: DashboardActionCardSurface(
        title: 'Criar Quiz',
        subtitle: 'Avaliações interativas',
        icon: Icons.quiz,
        minHeight: _cardMinHeight,
        titleFontSize: _titleFontSize,
        subtitleFontSize: _subtitleFontSize,
        iconSize: _iconSize,
        padding: _cardPadding,
        onTap: () => context.go('/teacher/quiz/create'),
      ),
    );
  }

  Widget _buildViewAnalyticsCard(BuildContext context) {
    final cs = Theme.of(context).colorScheme;
    return DashboardActionCardSurface(
      title: 'Analytics',
      subtitle: 'Desempenho da turma',
      icon: Icons.analytics,
      minHeight: _cardMinHeight,
      titleFontSize: _titleFontSize,
      subtitleFontSize: _subtitleFontSize,
      iconSize: _iconSize,
      padding: _cardPadding,
      iconColor: cs.primary,
      leadingWidget: Container(
        padding: const EdgeInsets.all(_iconPadding),
        decoration: BoxDecoration(
          color: cs.primary.withOpacity(0.1),
          borderRadius: BorderRadius.circular(10),
        ),
        child: const Icon(Icons.analytics, color: Colors.blue, size: 28),
      ),
      onTap: () => context.go('/teacher/analytics'),
    );
  }

  Widget _buildCreateClassCard(BuildContext context) {
    final cs = Theme.of(context).colorScheme;
    return ClipRRect(
      borderRadius: BorderRadius.circular(16),
      child: DashboardActionCardSurface(
        title: 'Criar Turma',
        subtitle: 'Gerar código de acesso',
        icon: Icons.school,
        minHeight: _cardMinHeight,
        titleFontSize: _titleFontSize,
        subtitleFontSize: _subtitleFontSize,
        iconSize: _iconSize,
        padding: _cardPadding,
        iconColor: cs.primary,
        onTapDisabled: _isCreatingClass,
        onTap: () => _showCreateClassDialog(context),
        leadingWidget: Container(
          padding: const EdgeInsets.all(_iconPadding),
          decoration: BoxDecoration(
            color: cs.primary.withOpacity(0.1),
            borderRadius: BorderRadius.circular(10),
          ),
          child: _isCreatingClass
              ? SizedBox(
                  width: _iconSize,
                  height: _iconSize,
                  child: CircularProgressIndicator(
                    strokeWidth: 2,
                    color: cs.primary,
                  ),
                )
              : Icon(Icons.school, color: cs.primary, size: _iconSize),
        ),
      ),
    );
  }

  void _showQuickActionsList(BuildContext context) {
    final cs = Theme.of(context).colorScheme;
    final items = [
      _TeacherActionItem(
        title: 'Upload Conteúdo',
        subtitle: 'PDFs, textos, imagens',
        icon: Icons.upload_file,
        useGradient: true,
        onTap: () {
          Navigator.pop(context);
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) => const TeacherMaterialsPage()),
          );
        },
      ),
      _TeacherActionItem(
        title: 'Criar Turma',
        subtitle: 'Gerar código de acesso',
        icon: Icons.school,
        onTap: () {
          Navigator.pop(context);
          _showCreateClassDialog(context);
        },
      ),
      _TeacherActionItem(
        title: 'Criar Quiz',
        subtitle: 'Avaliações interativas',
        icon: Icons.quiz,
        onTap: () {
          Navigator.pop(context);
          context.go('/teacher/quiz/create');
        },
      ),
      _TeacherActionItem(
        title: 'Analytics',
        subtitle: 'Desempenho da turma',
        icon: Icons.analytics,
        onTap: () {
          Navigator.pop(context);
          context.go('/teacher/analytics');
        },
      ),
    ];

    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
      ),
      builder: (context) => DraggableScrollableSheet(
        initialChildSize: 0.6,
        minChildSize: 0.3,
        maxChildSize: 0.9,
        expand: false,
        builder: (context, scrollController) {
          return Column(
            children: [
              Container(
                margin: const EdgeInsets.only(top: 8),
                width: 40,
                height: 4,
                decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(20),
                child: Text(
                  'Ações Rápidas',
                  style: TextStyle(
                    color: cs.onSurface,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Expanded(
                child: ListView.builder(
                  controller: scrollController,
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  itemCount: items.length,
                  itemBuilder: (context, index) {
                    final item = items[index];
                    return Card(
                      margin: const EdgeInsets.only(bottom: 12),
                      elevation: 0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12),
                        side: BorderSide(color: cs.outline.withOpacity(0.2)),
                      ),
                      child: ListTile(
                        leading: Container(
                          padding: const EdgeInsets.all(10),
                          decoration: BoxDecoration(
                            color: item.useGradient
                                ? cs.primary.withOpacity(0.1)
                                : cs.secondary.withOpacity(0.1),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Icon(
                            item.icon,
                            color: item.useGradient ? cs.primary : cs.secondary,
                            size: 24,
                          ),
                        ),
                        title: Text(
                          item.title,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            color: cs.onSurface,
                          ),
                        ),
                        subtitle: Text(
                          item.subtitle,
                          style: TextStyle(
                            color: cs.onSurfaceVariant,
                            fontSize: 13,
                          ),
                        ),
                        trailing: Icon(
                          Icons.arrow_forward_ios,
                          color: cs.primary,
                          size: 16,
                        ),
                        onTap: item.onTap,
                      ),
                    );
                  },
                ),
              ),
              const SizedBox(height: 20),
            ],
          );
        },
      ),
    );
  }

  void _showCreateClassDialog(BuildContext context) {
    final TextEditingController nameController = TextEditingController();
    String? selectedSchoolClassId;
    List<Map<String, String>> schoolClasses = [];
    bool isLoadingClasses = true;

    // Carregar school_classes antes de mostrar o dialog
    FirebaseFirestore.instance
        .collection('school_classes')
        .where('active', isEqualTo: true)
        .orderBy('year')
        .orderBy('section')
        .get()
        .then((snapshot) {
          schoolClasses = snapshot.docs.map((doc) {
            final data = doc.data();
            return {'id': doc.id, 'name': (data['name'] as String? ?? doc.id)};
          }).toList();
          isLoadingClasses = false;
        })
        .catchError((_) {
          isLoadingClasses = false;
        });

    showDialog(
      context: context,
      builder: (BuildContext dialogContext) {
        return StatefulBuilder(
          builder: (context, setDialogState) {
            // Atualizar estado do dialog quando as classes carregarem
            if (isLoadingClasses) {
              Future.delayed(const Duration(milliseconds: 300), () {
                if (dialogContext.mounted) setDialogState(() {});
              });
            }

            return AlertDialog(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(16),
              ),
              title: Text(
                'Criar Nova Turma',
                style: TextStyle(
                  color: Theme.of(context).colorScheme.onSurface,
                  fontWeight: FontWeight.bold,
                ),
              ),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Nome da turma:',
                    style: TextStyle(
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                      fontSize: 14,
                    ),
                  ),
                  const SizedBox(height: 12),
                  TextField(
                    controller: nameController,
                    decoration: InputDecoration(
                      hintText: 'Ex: Matemática, Física...',
                      filled: true,
                      fillColor: Theme.of(
                        context,
                      ).colorScheme.surfaceContainerHighest,
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide.none,
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide.none,
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(
                          color: Theme.of(context).colorScheme.primary,
                          width: 2,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: 16),
                  Text(
                    'Ano letivo:',
                    style: TextStyle(
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                      fontSize: 14,
                    ),
                  ),
                  const SizedBox(height: 8),
                  isLoadingClasses
                      ? Row(
                          children: [
                            SizedBox(
                              width: 16,
                              height: 16,
                              child: CircularProgressIndicator(
                                strokeWidth: 2,
                                color: Theme.of(context).colorScheme.primary,
                              ),
                            ),
                            const SizedBox(width: 10),
                            Text(
                              'A carregar turmas...',
                              style: TextStyle(
                                color: Theme.of(
                                  context,
                                ).colorScheme.onSurfaceVariant,
                                fontSize: 13,
                              ),
                            ),
                          ],
                        )
                      : DropdownButtonFormField<String>(
                          value: selectedSchoolClassId,
                          isExpanded: true,
                          decoration: InputDecoration(
                            filled: true,
                            fillColor: Theme.of(
                              context,
                            ).colorScheme.surfaceContainerHighest,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(12),
                              borderSide: BorderSide.none,
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(12),
                              borderSide: BorderSide.none,
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(12),
                              borderSide: BorderSide(
                                color: Theme.of(context).colorScheme.primary,
                                width: 2,
                              ),
                            ),
                            contentPadding: const EdgeInsets.symmetric(
                              horizontal: 12,
                              vertical: 10,
                            ),
                          ),
                          hint: Text(
                            'Seleciona o ano letivo',
                            style: TextStyle(
                              color: Theme.of(
                                context,
                              ).colorScheme.onSurfaceVariant,
                            ),
                          ),
                          items: schoolClasses
                              .map(
                                (c) => DropdownMenuItem<String>(
                                  value: c['id'],
                                  child: Text(c['name']!),
                                ),
                              )
                              .toList(),
                          onChanged: (value) => setDialogState(
                            () => selectedSchoolClassId = value,
                          ),
                        ),
                ],
              ),
              actions: [
                TextButton(
                  onPressed: () => Navigator.of(dialogContext).pop(),
                  child: Text(
                    'Cancelar',
                    style: TextStyle(
                      color: Theme.of(context).colorScheme.onSurfaceVariant,
                    ),
                  ),
                ),
                ElevatedButton(
                  onPressed: () {
                    final className = nameController.text.trim();
                    if (className.isNotEmpty) {
                      Navigator.of(dialogContext).pop();
                      _createClass(
                        className,
                        schoolClassId: selectedSchoolClassId,
                      );
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Theme.of(context).colorScheme.primary,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: const Text('Criar'),
                ),
              ],
            );
          },
        );
      },
    );
  }

  String _generateClassCode() {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    final random = Random();
    return String.fromCharCodes(
      Iterable.generate(
        6,
        (_) => chars.codeUnitAt(random.nextInt(chars.length)),
      ),
    );
  }

  Future<void> _createClass(String className, {String? schoolClassId}) async {
    setState(() {
      _isCreatingClass = true;
    });

    try {
      final currentUser = AuthService.currentUser;
      if (currentUser == null) {
        throw Exception('Utilizador não autenticado');
      }

      final classCode = _generateClassCode();
      final firestore = FirebaseFirestore.instance;

      final Map<String, dynamic> classData = {
        'name': className,
        'teacherId': currentUser.uid,
        'code': classCode,
        'createdAt': FieldValue.serverTimestamp(),
      };
      if (schoolClassId != null && schoolClassId.isNotEmpty) {
        classData['schoolClassId'] = schoolClassId;
      }

      await firestore.collection('classes').add(classData);

      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(
              'Turma "$className" criada com sucesso! Código: $classCode',
            ),
            backgroundColor: Theme.of(context).colorScheme.primary,
            behavior: SnackBarBehavior.floating,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Erro ao criar turma: $e'),
            backgroundColor: Colors.red,
            behavior: SnackBarBehavior.floating,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          ),
        );
      }
    } finally {
      setState(() {
        _isCreatingClass = false;
      });
    }
  }
}

class _TeacherActionItem {
  final String title;
  final String subtitle;
  final IconData icon;
  final bool useGradient;
  final VoidCallback onTap;

  _TeacherActionItem({
    required this.title,
    required this.subtitle,
    required this.icon,
    this.useGradient = false,
    required this.onTap,
  });
}
