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

/// URL input field implementation using ModernTextField
class UrlField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;

  UrlField({
    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 uri = Uri.tryParse(value);
          if (uri == null || !(uri.hasScheme && uri.hasAuthority)) {
            return 'Please enter a valid URL';
          }
        }
        return null;
      };

  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    return ModernTextField(
      controller: controller,
      hint: hint,
      keyboardType: TextInputType.url,
      prefixIcon: const Icon(Icons.link_rounded),
      validator: validator,
      onChanged: onChanged != null ? (_) => onChanged() : null,
    );
  }
}
