import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:file_selector/file_selector.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as path;

import '../../../../core/services/auth_service.dart';

/// Página de Materiais do Professor
/// Tela dedicada para upload e gestão de materiais para a IA
class TeacherMaterialsPage extends StatefulWidget {
  const TeacherMaterialsPage({super.key});

  @override
  State<TeacherMaterialsPage> createState() => _TeacherMaterialsPageState();
}

class _TeacherMaterialsPageState extends State<TeacherMaterialsPage> {
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;
  final FirebaseStorage _storage = FirebaseStorage.instanceFor(
    bucket: 'teachit-app.firebasestorage.app',
  );
  final ImagePicker _imagePicker = ImagePicker();
  bool _isUploading = false;

  Stream<QuerySnapshot> _getMaterialsStream() {
    final currentUser = AuthService.currentUser;
    if (currentUser == null) {
      return const Stream.empty();
    }

    return _firestore
        .collection('materials')
        .where('teacherId', isEqualTo: currentUser.uid)
        .orderBy('createdAt', descending: true)
        .snapshots();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Materiais da Disciplina',
          style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
        ),
        backgroundColor: const Color(0xFF82C9BD),
        elevation: 0,
        iconTheme: const IconThemeData(color: Colors.white),
      ),
      floatingActionButton: _isUploading
          ? FloatingActionButton.extended(
              onPressed: null,
              backgroundColor: const Color(0xFFF68D2D).withOpacity(0.6),
              icon: const SizedBox(
                width: 20,
                height: 20,
                child: CircularProgressIndicator(
                  strokeWidth: 2,
                  color: Colors.white,
                ),
              ),
              label: const Text(
                'A enviar...',
                style: TextStyle(color: Colors.white),
              ),
            )
          : FloatingActionButton.extended(
              onPressed: _showUploadOptions,
              backgroundColor: const Color(0xFFF68D2D),
              icon: const Icon(Icons.add, color: Colors.white),
              label: const Text(
                'Adicionar',
                style: TextStyle(color: Colors.white),
              ),
            ),

      body: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [Color(0xFF82C9BD), Color(0xFFF8F9FA)],
            stops: [0.0, 0.4],
          ),
        ),
        child: SafeArea(
          child: StreamBuilder<QuerySnapshot>(
            stream: _getMaterialsStream(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const Center(
                  child: CircularProgressIndicator(color: Color(0xFF82C9BD)),
                );
              }

              if (snapshot.hasError) {
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(
                        Icons.error_outline,
                        color: Colors.red,
                        size: 48,
                      ),
                      const SizedBox(height: 16),
                      Text(
                        'Erro ao carregar materiais:\n${snapshot.error}',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Theme.of(context).colorScheme.onSurface,
                          fontSize: 16,
                        ),
                      ),
                    ],
                  ),
                );
              }

              final materials = snapshot.data?.docs ?? [];

              if (materials.isEmpty) {
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.folder_open,
                        color: Theme.of(context).colorScheme.onSurfaceVariant,
                        size: 64,
                      ),
                      const SizedBox(height: 16),
                      Text(
                        'Nenhum material enviado ainda.',
                        style: TextStyle(
                          color: Theme.of(context).colorScheme.onSurfaceVariant,
                          fontSize: 16,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text(
                        'Os materiais enviados aparecerão aqui.',
                        style: TextStyle(
                          color: Theme.of(context).colorScheme.onSurfaceVariant,
                          fontSize: 14,
                        ),
                      ),
                    ],
                  ),
                );
              }

              return ListView.builder(
                padding: const EdgeInsets.all(16),
                itemCount: materials.length,
                itemBuilder: (context, index) {
                  final material =
                      materials[index].data() as Map<String, dynamic>;
                  final fileName = material['fileName'] ?? 'Ficheiro sem nome';
                  final createdAt = material['createdAt'] as Timestamp?;
                  // Inferir tipo pela extensão do filename
                  final extension = path.extension(fileName).toLowerCase();
                  final fileType = extension == '.pdf'
                      ? 'pdf'
                      : (extension == '.jpg' ||
                            extension == '.jpeg' ||
                            extension == '.png')
                      ? 'image'
                      : 'other';

                  final docId = materials[index].id;
                  final url = material['url'] as String?;
                  final classId = material['classId'] as String?;

                  return _buildMaterialCard(
                    docId: docId,
                    fileName: fileName,
                    fileType: fileType,
                    createdAt: createdAt,
                    url: url,
                    classId: classId,
                  );
                },
              );
            },
          ),
        ),
      ),
    );
  }

  void _showUploadOptions() {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) => Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
        ),
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(20),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Center(
                  child: Container(
                    width: 40,
                    height: 4,
                    decoration: BoxDecoration(
                      color: Colors.grey[300],
                      borderRadius: BorderRadius.circular(2),
                    ),
                  ),
                ),
                const SizedBox(height: 20),
                Text(
                  'Adicionar Material',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: Theme.of(context).colorScheme.onSurface,
                  ),
                ),
                const SizedBox(height: 20),
                _buildUploadOption(
                  icon: Icons.picture_as_pdf,
                  color: Colors.red,
                  title: 'PDF',
                  subtitle: 'Selecionar ficheiro PDF',
                  onTap: () {
                    Navigator.pop(context);
                    _selectPDF();
                  },
                ),
                const SizedBox(height: 12),
                _buildUploadOption(
                  icon: Icons.image,
                  color: Colors.blue,
                  title: 'Imagem da Galeria',
                  subtitle: 'Escolher foto existente',
                  onTap: () {
                    Navigator.pop(context);
                    _selectImageFromGallery();
                  },
                ),
                const SizedBox(height: 12),
                _buildUploadOption(
                  icon: Icons.camera_alt,
                  color: const Color(0xFF82C9BD),
                  title: 'Foto da Câmara',
                  subtitle: 'Tirar foto nova',
                  onTap: () {
                    Navigator.pop(context);
                    _takePhotoWithCamera();
                  },
                ),
                const SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildUploadOption({
    required IconData icon,
    required Color color,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(12),
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: color.withOpacity(0.05),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(color: color.withOpacity(0.2), width: 1),
        ),
        child: Row(
          children: [
            Container(
              width: 48,
              height: 48,
              decoration: BoxDecoration(
                color: color.withOpacity(0.1),
                borderRadius: BorderRadius.circular(12),
              ),
              child: Icon(icon, color: color, size: 24),
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: Theme.of(context).colorScheme.onSurface,
                    ),
                  ),
                  const SizedBox(height: 2),
                  Text(
                    subtitle,
                    style: TextStyle(fontSize: 13, color: Colors.grey[600]),
                  ),
                ],
              ),
            ),
            Icon(Icons.arrow_forward_ios, color: Colors.grey[400], size: 16),
          ],
        ),
      ),
    );
  }

  Future<void> _selectPDF() async {
    try {
      const XTypeGroup pdfTypeGroup = XTypeGroup(
        label: 'PDFs',
        extensions: ['pdf'],
        uniformTypeIdentifiers: ['com.adobe.pdf'],
      );

      final XFile? file = await openFile(acceptedTypeGroups: [pdfTypeGroup]);
      if (file == null) return;

      await _pickClassAndUpload(
        filePath: file.path,
        fileName: path.basename(file.path),
        fileType: 'pdf',
      );
    } catch (e) {
      if (mounted) {
        _showErrorSnackBar('Erro ao selecionar PDF: $e');
      }
    }
  }

  Future<void> _selectImageFromGallery() async {
    try {
      final XFile? image = await _imagePicker.pickImage(
        source: ImageSource.gallery,
        maxWidth: 1920,
        maxHeight: 1080,
        imageQuality: 85,
      );
      if (image == null) return;

      await _pickClassAndUpload(
        filePath: image.path,
        fileName: path.basename(image.path),
        fileType: 'image',
      );
    } catch (e) {
      if (mounted) {
        _showErrorSnackBar('Erro ao selecionar imagem: $e');
      }
    }
  }

  Future<void> _takePhotoWithCamera() async {
    try {
      final XFile? photo = await _imagePicker.pickImage(
        source: ImageSource.camera,
        maxWidth: 1920,
        maxHeight: 1080,
        imageQuality: 85,
      );
      if (photo == null) return;

      await _pickClassAndUpload(
        filePath: photo.path,
        fileName: path.basename(photo.path),
        fileType: 'image',
      );
    } catch (e) {
      if (mounted) {
        _showErrorSnackBar('Erro ao tirar foto: $e');
      }
    }
  }

  Future<void> _pickClassAndUpload({
    required String filePath,
    required String fileName,
    required String fileType,
  }) async {
    final currentUser = AuthService.currentUser;
    if (currentUser == null) {
      _showErrorSnackBar('Utilizador não autenticado');
      return;
    }

    // Carregar as disciplinas do professor
    List<Map<String, String>> teacherClasses = [];
    try {
      final snapshot = await _firestore
          .collection('classes')
          .where('teacherId', isEqualTo: currentUser.uid)
          .orderBy('createdAt', descending: true)
          .get();
      teacherClasses = snapshot.docs.map((doc) {
        final data = doc.data();
        return {'id': doc.id, 'name': (data['name'] as String? ?? doc.id)};
      }).toList();
    } catch (_) {}

    if (!mounted) return;

    // Se o professor não tem disciplinas, fazer upload sem associar disciplina
    if (teacherClasses.isEmpty) {
      await _uploadFile(
        filePath: filePath,
        fileName: fileName,
        fileType: fileType,
      );
      return;
    }

    // Mostrar diálogo de seleção de disciplina
    String? selectedClassId = await showDialog<String>(
      context: context,
      builder: (dialogContext) {
        String? picked;
        return StatefulBuilder(
          builder: (context, setDialogState) => AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(16),
            ),
            title: const Text(
              'Escolher Disciplina',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  'Seleciona a disciplina que terá acesso a este material:',
                  style: TextStyle(fontSize: 14),
                ),
                const SizedBox(height: 16),
                DropdownButtonFormField<String>(
                  value: picked,
                  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: const Text('Seleciona a disciplina'),
                  items: teacherClasses
                      .map(
                        (c) => DropdownMenuItem<String>(
                          value: c['id'],
                          child: Text(c['name']!),
                        ),
                      )
                      .toList(),
                  onChanged: (value) => setDialogState(() => picked = value),
                ),
              ],
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.of(dialogContext).pop(null),
                child: const Text('Cancelar'),
              ),
              ElevatedButton(
                onPressed: picked == null
                    ? null
                    : () => Navigator.of(dialogContext).pop(picked),
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFFF68D2D),
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
                child: const Text('Confirmar'),
              ),
            ],
          ),
        );
      },
    );

    // Se cancelou o diálogo, não fazer upload
    if (selectedClassId == null) return;

    await _uploadFile(
      filePath: filePath,
      fileName: fileName,
      fileType: fileType,
      classId: selectedClassId,
    );
  }

  Future<void> _uploadFile({
    required String filePath,
    required String fileName,
    required String fileType,
    String? classId,
  }) async {
    final currentUser = FirebaseAuth.instance.currentUser;
    if (currentUser == null) {
      _showErrorSnackBar('Utilizador não autenticado');
      return;
    }

    final uid = currentUser.uid;
    final cleanFileName = path.basename(fileName);

    setState(() => _isUploading = true);

    try {
      // Upload para Firebase Storage: teachers/{uid}/materials/{fileName}
      final storage = FirebaseStorage.instanceFor(
        bucket: 'teachit-app.firebasestorage.app',
      );
      final ref = storage
          .ref()
          .child('teachers')
          .child(uid)
          .child('materials')
          .child(cleanFileName);

      await ref.putFile(File(filePath));
      final downloadUrl = await ref.getDownloadURL();

      // Criar documento no Firestore
      final materialData = <String, dynamic>{
        'teacherId': uid,
        'fileName': cleanFileName,
        'url': downloadUrl,
        'createdAt': FieldValue.serverTimestamp(),
      };
      if (classId != null && classId.isNotEmpty) {
        materialData['classId'] = classId;
      }
      await FirebaseFirestore.instance
          .collection('materials')
          .add(materialData);

      if (mounted) {
        _showSuccessSnackBar('Material enviado com sucesso!');
      }
    } catch (e) {
      if (mounted) {
        _showErrorSnackBar('Erro ao enviar material: $e');
      }
    } finally {
      if (mounted) {
        setState(() => _isUploading = false);
      }
    }
  }

  Future<void> _deleteMaterial({
    required String docId,
    required String fileName,
    String? url,
  }) async {
    try {
      // Apagar ficheiro do Firebase Storage
      if (url != null && url.isNotEmpty) {
        try {
          final ref = _storage.refFromURL(url);
          await ref.delete();
        } catch (_) {
          // Se o ficheiro já não existir no Storage, continuar na mesma
        }
      }

      // Apagar documento no Firestore
      await _firestore.collection('materials').doc(docId).delete();

      if (mounted) {
        _showSuccessSnackBar('Material eliminado com sucesso!');
      }
    } catch (e) {
      if (mounted) {
        _showErrorSnackBar('Erro ao eliminar material: $e');
      }
    }
  }

  void _showDeleteConfirmation({
    required String docId,
    required String fileName,
    String? url,
  }) {
    showDialog(
      context: context,
      builder: (dialogContext) => AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        title: const Text(
          'Eliminar Material',
          style: TextStyle(fontWeight: FontWeight.bold),
        ),
        content: Text(
          'Tens a certeza que queres eliminar "$fileName"?\nEsta ação não pode ser desfeita.',
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(dialogContext).pop(),
            child: const Text('Cancelar'),
          ),
          ElevatedButton(
            onPressed: () {
              Navigator.of(dialogContext).pop();
              _deleteMaterial(docId: docId, fileName: fileName, url: url);
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.red,
              foregroundColor: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10),
              ),
            ),
            child: const Text('Eliminar'),
          ),
        ],
      ),
    );
  }

  void _showSuccessSnackBar(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Row(
          children: [
            const Icon(Icons.check_circle, color: Colors.white),
            const SizedBox(width: 12),
            Expanded(child: Text(message)),
          ],
        ),
        backgroundColor: const Color(0xFF10B981),
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
        duration: const Duration(seconds: 2),
      ),
    );
  }

  void _showErrorSnackBar(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Row(
          children: [
            const Icon(Icons.error_outline, color: Colors.white),
            const SizedBox(width: 12),
            Expanded(child: Text(message)),
          ],
        ),
        backgroundColor: Colors.red,
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
        duration: const Duration(seconds: 3),
      ),
    );
  }

  Future<String?> _getClassName(String classId) async {
    try {
      final doc = await _firestore.collection('classes').doc(classId).get();
      if (doc.exists) {
        return doc.data()?['name'] as String?;
      }
      return null;
    } catch (_) {
      return null;
    }
  }

  Widget _buildMaterialCard({
    required String docId,
    required String fileName,
    required String fileType,
    required Timestamp? createdAt,
    String? url,
    String? classId,
  }) {
    IconData iconData;
    Color iconColor;

    switch (fileType.toLowerCase()) {
      case 'pdf':
        iconData = Icons.picture_as_pdf;
        iconColor = Colors.red;
        break;
      case 'image':
      case 'jpg':
      case 'jpeg':
      case 'png':
        iconData = Icons.image;
        iconColor = Colors.blue;
        break;
      default:
        iconData = Icons.insert_drive_file;
        iconColor = const Color(0xFF82C9BD);
    }

    String formattedDate = 'Data desconhecida';
    if (createdAt != null) {
      formattedDate = DateFormat('dd/MM/yyyy HH:mm').format(createdAt.toDate());
    }

    return Card(
      margin: const EdgeInsets.only(bottom: 12),
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 12,
        ),
        leading: Container(
          width: 48,
          height: 48,
          decoration: BoxDecoration(
            color: iconColor.withOpacity(0.1),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Icon(iconData, color: iconColor, size: 28),
        ),
        title: Text(
          fileName,
          style: TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: 16,
            color: Theme.of(context).colorScheme.onSurface,
          ),
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
        ),
        subtitle: classId != null
            ? FutureBuilder<String?>(
                future: _getClassName(classId),
                builder: (context, snap) {
                  final className = snap.data;
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        formattedDate,
                        style: TextStyle(
                          color: Theme.of(context).colorScheme.onSurfaceVariant,
                          fontSize: 13,
                        ),
                      ),
                      if (className != null) ...[
                        const SizedBox(height: 2),
                        Row(
                          children: [
                            Icon(
                              Icons.school_outlined,
                              size: 12,
                              color: Theme.of(context).colorScheme.primary,
                            ),
                            const SizedBox(width: 4),
                            Flexible(
                              child: Text(
                                className,
                                style: TextStyle(
                                  color: Theme.of(context).colorScheme.primary,
                                  fontSize: 12,
                                  fontWeight: FontWeight.w500,
                                ),
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ],
                  );
                },
              )
            : Text(
                formattedDate,
                style: TextStyle(
                  color: Theme.of(context).colorScheme.onSurfaceVariant,
                  fontSize: 13,
                ),
              ),
        trailing: IconButton(
          icon: const Icon(Icons.delete_outline, color: Colors.red),
          tooltip: 'Eliminar',
          onPressed: () => _showDeleteConfirmation(
            docId: docId,
            fileName: fileName,
            url: url,
          ),
        ),
      ),
    );
  }
}
