import 'package:flutter/material.dart';
import 'package:playmaker/controllers/login_controller.dart';
import '../widgets/login_widgets.dart';
import 'home.dart'; // <--- IMPORTANTE: Importa a tua HomeScreen

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final LoginController controller = LoginController();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: ListenableBuilder(
          listenable: controller,
          builder: (context, child) {
            return LayoutBuilder(
              builder: (context, constraints) {
                final screenWidth = constraints.maxWidth;
                
                return Center(
                  child: SingleChildScrollView( 
                    child: Container(
                      width: screenWidth * 0.6,
                      constraints: const BoxConstraints(minWidth: 340),
                      padding: const EdgeInsets.all(32),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          const BasketTrackHeader(),
                          const SizedBox(height: 40),
                          
                          LoginFormFields(controller: controller),
                          const SizedBox(height: 24),
                          
                          // AQUI ESTÁ A MUDANÇA PRINCIPAL
                          LoginButton(
                            controller: controller,
                            onLoginSuccess: () {
                              // Verifica se o widget ainda está no ecrã antes de navegar
                              if (mounted) {
                                Navigator.pushReplacement(
                                  context,
                                  MaterialPageRoute(builder: (context) => const HomeScreen()),
                                );
                              }
                            },
                          ),
                          const SizedBox(height: 16),
                          
                          const CreateAccountButton(),
                        ],
                      ),
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }
}