import 'package:flutter/material.dart'; import 'package:flutter_animate/flutter_animate.dart'; import 'package:go_router/go_router.dart'; import '../../../../l10n/app_localizations.dart'; import '../../../../core/services/auth_service.dart'; import '../../../../shared/presentation/widgets/custom_notification.dart'; class SignupPage extends StatefulWidget { final String? selectedRole; const SignupPage({super.key, this.selectedRole}); @override State createState() => _SignupPageState(); } class _SignupPageState extends State { final _formKey = GlobalKey(); final _nameController = TextEditingController(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; bool _obscurePassword = true; late String _selectedRole; @override void initState() { super.initState(); // Usar role passado da tela anterior ou default 'student' _selectedRole = widget.selectedRole ?? 'student'; } @override void dispose() { _nameController.dispose(); _emailController.dispose(); _passwordController.dispose(); super.dispose(); } Future _handleSignup() async { if (!_formKey.currentState!.validate()) return; setState(() { _isLoading = true; }); try { // Get name, email and password from controllers final name = _nameController.text.trim(); final email = _emailController.text.trim(); final password = _passwordController.text.trim(); print('DEBUG: Iniciando processo de signup para: $email'); print('DEBUG: Nome: $name, Papel: $_selectedRole'); // Attempt signup with Firebase await AuthService.signUpWithEmailAndPassword( email: email, password: password, displayName: name, role: _selectedRole, ); print('DEBUG: Signup Firebase bem-sucedido, navegando para dashboard'); if (mounted) { setState(() { _isLoading = false; }); // Show success message NotificationHelper.showSuccess( context, message: 'Conta criada com sucesso!', ); // Navigate to appropriate dashboard based on role if (_selectedRole == 'teacher') { context.go('/teacher-dashboard'); } else { context.go('/student-dashboard'); } } } catch (e) { print('DEBUG: Erro no signup: $e'); if (mounted) { setState(() { _isLoading = false; }); // Show error message NotificationHelper.showError( context, message: e.toString().replaceAll('Exception: ', ''), ); } } } @override Widget build(BuildContext context) { return PopScope( canPop: false, onPopInvokedWithResult: (didPop, result) { if (!didPop) { context.go('/role-selection'); } }, child: Scaffold( body: Stack( children: [ // Main content Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [ Theme.of(context).colorScheme.background, Theme.of(context).colorScheme.primary.withOpacity(0.1), Theme.of(context).colorScheme.secondary.withOpacity(0.05), Theme.of(context).colorScheme.background, ], ), ), child: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const SizedBox(height: 60), // Logo/Title Container( padding: const EdgeInsets.all(20.0), decoration: BoxDecoration( color: Theme.of( context, ).colorScheme.surface.withOpacity(0.9), borderRadius: BorderRadius.circular(16.0), boxShadow: [ BoxShadow( color: Theme.of( context, ).colorScheme.shadow.withOpacity(0.1), blurRadius: 10.0, offset: const Offset(0, 4), ), ], ), child: Column( children: [ Text( 'EPVC', style: TextStyle( fontSize: 32, fontWeight: FontWeight.bold, foreground: Paint() ..shader = LinearGradient( colors: [ Theme.of( context, ).colorScheme.primary, Theme.of( context, ).colorScheme.secondary, ], ).createShader( Rect.fromLTWH(0, 0, 200, 20), ), ), ), const SizedBox(height: 8), Text( 'Escola Profissional de Vila do Conde', style: TextStyle( fontSize: 14, color: Theme.of( context, ).colorScheme.onSurface, fontWeight: FontWeight.w500, ), ), ], ), ).animate().fadeIn( duration: const Duration(milliseconds: 800), ), const SizedBox(height: 40), // Signup form Container( padding: const EdgeInsets.all(24.0), decoration: BoxDecoration( color: Theme.of( context, ).colorScheme.surface.withOpacity(0.9), borderRadius: BorderRadius.circular(16.0), boxShadow: [ BoxShadow( color: Theme.of( context, ).colorScheme.shadow.withOpacity(0.1), blurRadius: 10.0, offset: const Offset(0, 4), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( 'Criar Conta', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Theme.of( context, ).colorScheme.onSurface, ), ), const SizedBox(height: 24), // Name field TextFormField( controller: _nameController, keyboardType: TextInputType.name, style: TextStyle( color: Theme.of( context, ).colorScheme.onSurface, ), decoration: InputDecoration( labelText: 'Nome Completo', labelStyle: TextStyle( color: Theme.of( context, ).colorScheme.onSurface, ), hintStyle: TextStyle( color: Theme.of( context, ).colorScheme.onSurfaceVariant, ), prefixIcon: Icon( Icons.person, color: Theme.of( context, ).colorScheme.primary, ), border: InputBorder.none, enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide( color: Theme.of( context, ).colorScheme.outline.withOpacity(0.3), ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide( color: Theme.of( context, ).colorScheme.primary, ), ), filled: true, fillColor: Theme.of(context).brightness == Brightness.dark ? Theme.of( context, ).colorScheme.surfaceContainerHighest : Theme.of(context).colorScheme.surface, ), validator: (value) { if (value == null || value.isEmpty) { return 'Nome é obrigatório'; } if (value.length < 2) { return 'Nome muito curto'; } return null; }, ), const SizedBox(height: 16), // Email field TextFormField( controller: _emailController, keyboardType: TextInputType.emailAddress, style: TextStyle( color: Theme.of( context, ).colorScheme.onSurface, ), decoration: InputDecoration( labelText: 'Email', labelStyle: TextStyle( color: Theme.of( context, ).colorScheme.onSurface, ), hintStyle: TextStyle( color: Theme.of( context, ).colorScheme.onSurfaceVariant, ), prefixIcon: Icon( Icons.email, color: Theme.of( context, ).colorScheme.primary, ), border: InputBorder.none, enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide( color: Theme.of( context, ).colorScheme.outline.withOpacity(0.3), ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide( color: Theme.of( context, ).colorScheme.primary, ), ), filled: true, fillColor: Theme.of(context).brightness == Brightness.dark ? Theme.of( context, ).colorScheme.surfaceContainerHighest : Theme.of(context).colorScheme.surface, ), validator: (value) { if (value == null || value.isEmpty) { return 'Email é obrigatório'; } if (!value.contains('@')) { return 'Email inválido'; } return null; }, ), const SizedBox(height: 16), // Password field TextFormField( controller: _passwordController, obscureText: _obscurePassword, style: TextStyle( color: Theme.of( context, ).colorScheme.onSurface, ), decoration: InputDecoration( labelText: 'Palavra-passe', labelStyle: TextStyle( color: Theme.of( context, ).colorScheme.onSurface, ), hintStyle: TextStyle( color: Theme.of( context, ).colorScheme.onSurfaceVariant, ), prefixIcon: Icon( Icons.lock, color: Theme.of( context, ).colorScheme.primary, ), suffixIcon: IconButton( icon: Icon( _obscurePassword ? Icons.visibility : Icons.visibility_off, color: Theme.of( context, ).colorScheme.primary, ), onPressed: () { setState(() { _obscurePassword = !_obscurePassword; }); }, ), border: InputBorder.none, enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide( color: Theme.of( context, ).colorScheme.outline.withOpacity(0.3), ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(8.0), borderSide: BorderSide( color: Theme.of( context, ).colorScheme.primary, ), ), filled: true, fillColor: Theme.of(context).brightness == Brightness.dark ? Theme.of( context, ).colorScheme.surfaceContainerHighest : Theme.of(context).colorScheme.surface, ), validator: (value) { if (value == null || value.isEmpty) { return 'Palavra-passe é obrigatória'; } if (value.length < 6) { return 'Palavra-passe muito curta'; } return null; }, ), const SizedBox(height: 24), // Signup button SizedBox( height: 50, child: ElevatedButton( onPressed: _isLoading ? null : _handleSignup, style: ElevatedButton.styleFrom( backgroundColor: Theme.of( context, ).colorScheme.primary, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular( 8.0, ), ), elevation: 2, ), child: _isLoading ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( strokeWidth: 2, valueColor: AlwaysStoppedAnimation( Colors.white, ), ), ) : Text( 'Criar Conta', style: const TextStyle( fontSize: 16, fontWeight: FontWeight.bold, ), ), ), ), const SizedBox(height: 16), // Login link GestureDetector( onTap: () { context.go('/login?role=$_selectedRole'); }, child: Text( 'Já tem conta? Entrar aqui', style: TextStyle( color: Theme.of( context, ).colorScheme.primary, fontWeight: FontWeight.w500, ), ), ), ], ), ).animate().fadeIn( duration: const Duration(milliseconds: 1000), ), const SizedBox(height: 40), ], ), ), ), ), ), ), // Custom back button Positioned( top: 50, left: 16, child: Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface.withOpacity(0.8), borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow( color: Theme.of( context, ).colorScheme.shadow.withOpacity(0.1), blurRadius: 8, offset: const Offset(0, 2), ), ], ), child: IconButton( icon: Icon( Icons.arrow_back, color: Theme.of(context).colorScheme.onSurface, ), onPressed: () => context.go('/role-selection'), ), ), ), ], ), ), ); } }