import 'package:flutter/material.dart';
import '../controllers/register_controller.dart'; // Garante que o caminho está certo

class RegisterHeader extends StatelessWidget {
  const RegisterHeader({super.key});

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    
    final logoSize = screenWidth > 600 ? 150.0 : 100.0; 
    final titleFontSize = screenWidth > 600 ? 48.0 : 36.0;
    final subtitleFontSize = screenWidth > 600 ? 22.0 : 18.0;

    return Column(
      children: [
        Icon(
          Icons.person_add_outlined, 
          size: logoSize, 
          color: const Color(0xFFE74C3C)
        ),
        const SizedBox(height: 10),
        Text(
          'Nova Conta',
          style: TextStyle(
            fontSize: titleFontSize,
            fontWeight: FontWeight.bold,
            color: Colors.grey[900],
          ),
        ),
        const SizedBox(height: 5),
        Text(
          'Cria o teu perfil no BasketTrack',
          style: TextStyle(
            fontSize: subtitleFontSize,
            color: Colors.grey[600],
            fontWeight: FontWeight.w500,
          ),
          textAlign: TextAlign.center,
        ),
      ],
    );
  }
}

class RegisterFormFields extends StatefulWidget {
  final RegisterController controller;

  const RegisterFormFields({super.key, required this.controller});

  @override
  State<RegisterFormFields> createState() => _RegisterFormFieldsState();
}

class _RegisterFormFieldsState extends State<RegisterFormFields> {
  bool _obscurePassword = true;

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    final verticalPadding = screenWidth > 600 ? 22.0 : 16.0;

    // IMPORTANTE: Envolvemos tudo num Form usando a chave do controller
    return Form(
      key: widget.controller.formKey, 
      child: Column(
        children: [
          // Campo Nome (Opcional, mas útil)
          TextFormField(
            controller: widget.controller.nameController,
            decoration: InputDecoration(
              labelText: 'Nome Completo',
              prefixIcon: const Icon(Icons.person_outline),
              border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
              contentPadding: EdgeInsets.symmetric(vertical: verticalPadding, horizontal: 16),
            ),
          ),
          const SizedBox(height: 20),

          // Campo Email
          TextFormField(
            controller: widget.controller.emailController,
            // Validação automática ligada ao controller
            validator: widget.controller.validateEmail, 
            decoration: InputDecoration(
              labelText: 'E-mail',
              prefixIcon: const Icon(Icons.email_outlined),
              border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
              contentPadding: EdgeInsets.symmetric(vertical: verticalPadding, horizontal: 16),
            ),
            keyboardType: TextInputType.emailAddress,
          ),
          const SizedBox(height: 20),

          // Campo Password
          TextFormField(
            controller: widget.controller.passwordController,
            obscureText: _obscurePassword,
            validator: widget.controller.validatePassword,
            decoration: InputDecoration(
              labelText: 'Palavra-passe',
              prefixIcon: const Icon(Icons.lock_outlined),
              suffixIcon: IconButton(
                icon: Icon(_obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined),
                onPressed: () => setState(() => _obscurePassword = !_obscurePassword),
              ),
              border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
              contentPadding: EdgeInsets.symmetric(vertical: verticalPadding, horizontal: 16),
            ),
          ),
          const SizedBox(height: 20),

          // Campo Confirmar Password
          TextFormField(
            controller: widget.controller.confirmPasswordController,
            obscureText: _obscurePassword,
            validator: widget.controller.validateConfirmPassword,
            decoration: InputDecoration(
              labelText: 'Confirmar Palavra-passe',
              prefixIcon: const Icon(Icons.lock_clock_outlined),
              border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
              contentPadding: EdgeInsets.symmetric(vertical: verticalPadding, horizontal: 16),
            ),
          ),
        ],
      ),
    );
  }
}

class RegisterButton extends StatelessWidget {
  final RegisterController controller;

  const RegisterButton({
    super.key,
    required this.controller,
  });

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    
    final buttonHeight = screenWidth > 600 ? 70.0 : 58.0;
    final fontSize = screenWidth > 600 ? 22.0 : 18.0;

    return SizedBox(
      width: double.infinity,
      height: buttonHeight,
      child: ElevatedButton(
        // Passamos o context para o controller lidar com as SnackBars e Navegação
        onPressed: controller.isLoading ? null : () => controller.signUp(context),
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color(0xFFE74C3C),
          foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(14),
          ),
          elevation: 3,
        ),
        child: controller.isLoading
            ? const SizedBox(
                width: 28,
                height: 28,
                child: CircularProgressIndicator(
                  strokeWidth: 3,
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                ),
              )
            : Text(
                'Criar Conta',
                style: TextStyle(
                  fontSize: fontSize,
                  fontWeight: FontWeight.w700,
                ),
              ),
      ),
    );
  }
}