import 'package:flutter/material.dart';
import 'base_field.dart';

/// Boolean switch field implementation
class SwitchField extends BaseField {
  final String fieldKey;
  final String label;
  final String hint;
  final bool isRequired;

  SwitchField({
    required this.fieldKey,
    required this.label,
    this.hint = '',
    this.isRequired = false,
  });

  @override
  String? Function(String?)? get validator => (_) => null;

  @override
  Widget buildField({
    required TextEditingController controller,
    required ColorScheme colorScheme,
    VoidCallback? onChanged,
  }) {
    // Set default value to true if controller is empty
    if (controller.text.isEmpty) {
      controller.text = 'true';
    }

    final bool current = (controller.text.toLowerCase() == 'true');
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(
          child: Text(
            label,
            style: TextStyle(
              color: colorScheme.onSurface,
              fontSize: 16,
              fontWeight: FontWeight.w600,
            ),
          ),
        ),
        Switch(
          value: current,
          activeColor: colorScheme.onPrimary,
          activeTrackColor: colorScheme.primary,
          onChanged: (value) {
            controller.text = value.toString();
            if (onChanged != null) onChanged();
          },
        ),
      ],
    );
  }
}
