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

/// Phone number input field implementation
class PhoneField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;
  final String? countryCode;
  
  PhoneField({
    required this.fieldKey,
    required this.label,
    required this.hint,
    this.isRequired = false,
    this.countryCode,
  });
  
  @override
  String? Function(String?)? get validator => (value) {
    if (isRequired && (value == null || value.isEmpty)) {
      return '$label is required';
    }
    if (value != null && value.isNotEmpty) {
      // Phone number validation
      final phoneRegex = RegExp(r'^\+?[\d\s\-\(\)]+$');
      if (!phoneRegex.hasMatch(value)) {
        return 'Please enter a valid phone number';
      }
    }
    return null;
  };
  
  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    return ModernTextField(
      controller: controller,
      hint: hint,
      keyboardType: TextInputType.phone,
      prefixIcon: countryCode != null ? Text(countryCode!) : null,
      onChanged: onChanged != null ? (_) => onChanged() : null,
      validator: validator,
    );
  }
}
