import 'package:flutter/material.dart';
import '../controllers/register_controller.dart'; 
import '../utils/size_extension.dart'; 
import 'dart:math' as math; // 👇 IMPORTANTE

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

  @override
  Widget build(BuildContext context) {
    final double safeSf = math.min(context.sf, 1.15); // TRAVÃO

    return Column(
      children: [
        Icon(Icons.person_add_outlined, size: 100 * safeSf, color: const Color(0xFFE74C3C)),
        SizedBox(height: 10 * safeSf),
        Text(
          'Nova Conta',
          style: TextStyle(fontSize: 36 * safeSf, fontWeight: FontWeight.bold, color: Colors.grey[900]),
        ),
        SizedBox(height: 5 * safeSf),
        Text(
          'Cria o teu perfil no BasketTrack',
          style: TextStyle(fontSize: 16 * safeSf, 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 double safeSf = math.min(context.sf, 1.15); // TRAVÃO

    return Container(
      constraints: BoxConstraints(maxWidth: 450 * safeSf), // LIMITA A LARGURA NO TABLET
      child: Form(
        key: widget.controller.formKey, 
        child: Column(
          children: [
            TextFormField(
              controller: widget.controller.nameController,
              style: TextStyle(fontSize: 15 * safeSf),
              decoration: InputDecoration(
                labelText: 'Nome Completo',
                labelStyle: TextStyle(fontSize: 15 * safeSf),
                prefixIcon: Icon(Icons.person_outline, size: 22 * safeSf),
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(12 * safeSf)),
                contentPadding: EdgeInsets.symmetric(vertical: 18 * safeSf, horizontal: 16 * safeSf),
              ),
            ),
            SizedBox(height: 20 * safeSf),

            TextFormField(
              controller: widget.controller.emailController,
              validator: widget.controller.validateEmail, 
              style: TextStyle(fontSize: 15 * safeSf),
              decoration: InputDecoration(
                labelText: 'E-mail',
                labelStyle: TextStyle(fontSize: 15 * safeSf),
                prefixIcon: Icon(Icons.email_outlined, size: 22 * safeSf),
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(12 * safeSf)),
                contentPadding: EdgeInsets.symmetric(vertical: 18 * safeSf, horizontal: 16 * safeSf),
              ),
              keyboardType: TextInputType.emailAddress,
            ),
            SizedBox(height: 20 * safeSf),

            TextFormField(
              controller: widget.controller.passwordController,
              obscureText: _obscurePassword,
              validator: widget.controller.validatePassword,
              style: TextStyle(fontSize: 15 * safeSf),
              decoration: InputDecoration(
                labelText: 'Palavra-passe',
                labelStyle: TextStyle(fontSize: 15 * safeSf),
                prefixIcon: Icon(Icons.lock_outlined, size: 22 * safeSf),
                suffixIcon: IconButton(
                  icon: Icon(_obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined, size: 22 * safeSf),
                  onPressed: () => setState(() => _obscurePassword = !_obscurePassword),
                ),
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(12 * safeSf)),
                contentPadding: EdgeInsets.symmetric(vertical: 18 * safeSf, horizontal: 16 * safeSf),
              ),
            ),
            SizedBox(height: 20 * safeSf),

            TextFormField(
              controller: widget.controller.confirmPasswordController,
              obscureText: _obscurePassword,
              validator: widget.controller.validateConfirmPassword,
              style: TextStyle(fontSize: 15 * safeSf),
              decoration: InputDecoration(
                labelText: 'Confirmar Palavra-passe',
                labelStyle: TextStyle(fontSize: 15 * safeSf),
                prefixIcon: Icon(Icons.lock_clock_outlined, size: 22 * safeSf),
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(12 * safeSf)),
                contentPadding: EdgeInsets.symmetric(vertical: 18 * safeSf, horizontal: 16 * safeSf),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class RegisterButton extends StatelessWidget {
  final RegisterController controller;
  const RegisterButton({super.key, required this.controller});

  @override
  Widget build(BuildContext context) {
    final double safeSf = math.min(context.sf, 1.15); // TRAVÃO

    return Container(
      constraints: BoxConstraints(maxWidth: 450 * safeSf), // LIMITA LARGURA
      height: 58 * safeSf,
      child: ElevatedButton(
        onPressed: controller.isLoading ? null : () => controller.signUp(context),
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color(0xFFE74C3C),
          foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14 * safeSf)),
          elevation: 3,
        ),
        child: controller.isLoading
            ? SizedBox(
                width: 28 * safeSf, height: 28 * safeSf,
                child: const CircularProgressIndicator(strokeWidth: 3, valueColor: AlwaysStoppedAnimation<Color>(Colors.white)),
              )
            : Text('Criar Conta', style: TextStyle(fontSize: 18 * safeSf, fontWeight: FontWeight.bold)),
      ),
    );
  }
}