import 'package:flutter/material.dart';
import 'package:playmaker/controllers/login_controller.dart';
import 'package:playmaker/pages/RegisterPage.dart';
import '../utils/size_extension.dart'; 
import 'dart:math' as math; // 👇 IMPORTANTE PARA O TRAVÃO NO TABLET!

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

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

    return Column(
      children: [
        SizedBox(
          width: 200 * safeSf, 
          height: 200 * safeSf,
          child: Image.asset(
            'assets/playmaker-logos.png',
            fit: BoxFit.contain,
          ),
        ),
        Text(
          'BasketTrack',
          style: TextStyle(
            fontSize: 36 * safeSf,
            fontWeight: FontWeight.bold,
            color: Colors.grey[900],
          ),
        ),
        SizedBox(height: 6 * safeSf), 
        Text(
          'Gere as tuas equipas e estatísticas',
          style: TextStyle(
            fontSize: 16 * safeSf,
            color: Colors.grey[600],
            fontWeight: FontWeight.w500,
          ),
          textAlign: TextAlign.center,
        ),
      ],
    );
  }
}

class LoginFormFields extends StatelessWidget {
  final LoginController controller;

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

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

    return Column(
      children: [
        TextField(
          controller: controller.emailController,
          style: TextStyle(fontSize: 15 * safeSf),
          decoration: InputDecoration(
            labelText: 'E-mail',
            labelStyle: TextStyle(fontSize: 15 * safeSf),
            prefixIcon: Icon(Icons.email_outlined, size: 22 * safeSf),
            errorText: controller.emailError, 
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(12 * safeSf)),
            contentPadding: EdgeInsets.symmetric(vertical: 18 * safeSf, horizontal: 16 * safeSf),
          ),
          keyboardType: TextInputType.emailAddress,
        ),
        SizedBox(height: 20 * safeSf),
        TextField(
          controller: controller.passwordController,
          obscureText: controller.obscurePassword,
          style: TextStyle(fontSize: 15 * safeSf),
          decoration: InputDecoration(
            labelText: 'Palavra-passe',
            labelStyle: TextStyle(fontSize: 15 * safeSf),
            prefixIcon: Icon(Icons.lock_outlined, size: 22 * safeSf),
            errorText: controller.passwordError,
            suffixIcon: IconButton(
              icon: Icon(
                controller.obscurePassword ? Icons.visibility_outlined : Icons.visibility_off_outlined,
                size: 22 * safeSf
              ),
              onPressed: controller.togglePasswordVisibility,
            ),
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(12 * safeSf)),
            contentPadding: EdgeInsets.symmetric(vertical: 18 * safeSf, horizontal: 16 * safeSf),
          ),
        ),
      ],
    );
  }
}

class LoginButton extends StatelessWidget {
  final LoginController controller;
  final VoidCallback onLoginSuccess;

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

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

    return SizedBox(
      width: double.infinity,
      height: 58 * safeSf,
      child: ElevatedButton(
        onPressed: controller.isLoading ? null : () async {
          final success = await controller.login();
          if (success) onLoginSuccess();
        },
        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('Entrar', style: TextStyle(fontSize: 18 * safeSf, fontWeight: FontWeight.bold)),
      ),
    );
  }
}

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

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

    return SizedBox(
      width: double.infinity,
      height: 58 * safeSf,
      child: OutlinedButton(
        onPressed: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) => const RegisterPage()));
        },
        style: OutlinedButton.styleFrom(
          foregroundColor: const Color(0xFFE74C3C),
          side: BorderSide(color: const Color(0xFFE74C3C), width: 2 * safeSf),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14 * safeSf)),
        ),
        child: Text('Criar Conta', style: TextStyle(fontSize: 18 * safeSf, fontWeight: FontWeight.bold)),
      ),
    );
  }
}