import 'package:flutter/material.dart';
import 'package:playmaker/controllers/login_controller.dart';
import '../widgets/login_widgets.dart';
import 'home.dart'; 
import '../utils/size_extension.dart'; 

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(
      // 👇 Adaptável ao Modo Claro/Escuro do Flutter
      backgroundColor: Theme.of(context).scaffoldBackgroundColor, 
      body: SafeArea(
        child: ListenableBuilder(
          listenable: controller,
          builder: (context, child) {
            return Center(
              child: SingleChildScrollView( 
                child: Container(
                  width: double.infinity,
                  constraints: BoxConstraints(maxWidth: 450 * context.sf), 
                  padding: EdgeInsets.all(32 * context.sf),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const BasketTrackHeader(),
                      SizedBox(height: 40 * context.sf),
                      
                      LoginFormFields(controller: controller),
                      SizedBox(height: 24 * context.sf),
                      
                      LoginButton(
                        controller: controller,
                        onLoginSuccess: () {
                          if (mounted) {
                            Navigator.pushReplacement(
                              context,
                              MaterialPageRoute(builder: (context) => const HomeScreen()),
                            );
                          }
                        },
                      ),
                      SizedBox(height: 16 * context.sf),
                      
                      const CreateAccountButton(),
                    ],
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}