import 'package:flutter/material.dart';
import 'base_field.dart';
import '../../../shared/widgets/inputs/modern_text_field.dart';

/// Email input field implementation
class EmailField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;
  
  EmailField({
    required this.fieldKey,
    required this.label,
    required this.hint,
    this.isRequired = false,
  });
  
  @override
  String? Function(String?)? get validator => (value) {
    if (isRequired && (value == null || value.isEmpty)) {
      return '$label is required';
    }
    if (value != null && value.isNotEmpty) {
      final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
      if (!emailRegex.hasMatch(value)) {
        return 'Please enter a valid email address';
      }
    }
    return null;
  };
  
  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    return ModernTextField(
      controller: controller,
      hint: hint,
      keyboardType: TextInputType.emailAddress,
      prefixIcon: const Icon(Icons.email_outlined),
      onChanged: onChanged != null ? (_) => onChanged() : null,
      validator: validator,
    );
  }
}
