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

/// Number input field implementation
class NumberField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;
  final double? min;
  final double? max;
  final int? decimalPlaces;
  
  NumberField({
    required this.fieldKey,
    required this.label,
    required this.hint,
    this.isRequired = false,
    this.min,
    this.max,
    this.decimalPlaces,
  });
  
  @override
  String? Function(String?)? get validator => (value) {
    if (isRequired && (value == null || value.isEmpty)) {
      return '$label is required';
    }
    if (value != null && value.isNotEmpty) {
      final number = double.tryParse(value);
      if (number == null) {
        return 'Please enter a valid number';
      }
      if (min != null && number < min!) {
        return '$label must be at least $min';
      }
      if (max != null && number > max!) {
        return '$label must be at most $max';
      }
    }
    return null;
  };
  
  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    return ModernTextField(
      controller: controller,
      hint: hint,
      keyboardType: TextInputType.numberWithOptions(
        decimal: decimalPlaces != null && decimalPlaces! > 0,
      ),
      onChanged: onChanged != null ? (_) => onChanged() : null,
      validator: validator,
    );
  }
}
