/// Widget de barre de recherche pour les organisations
/// Respecte le design system établi
library organisation_search_bar;

import 'package:flutter/material.dart';
import '../../../../../shared/design_system/tokens/app_colors.dart';
import '../../../../../shared/design_system/tokens/color_tokens.dart';

/// Barre de recherche avec design cohérent
class OrganisationSearchBar extends StatefulWidget {
  final TextEditingController controller;
  final Function(String) onSearch;
  final VoidCallback? onClear;
  final String hintText;
  final bool enabled;

  const OrganisationSearchBar({
    super.key,
    required this.controller,
    required this.onSearch,
    this.onClear,
    this.hintText = 'Rechercher une organisation...',
    this.enabled = true,
  });

  @override
  State<OrganisationSearchBar> createState() => _OrganisationSearchBarState();
}

class _OrganisationSearchBarState extends State<OrganisationSearchBar> {
  bool _hasText = false;

  @override
  void initState() {
    super.initState();
    widget.controller.addListener(_onTextChanged);
    _hasText = widget.controller.text.isNotEmpty;
  }

  @override
  void dispose() {
    widget.controller.removeListener(_onTextChanged);
    super.dispose();
  }

  void _onTextChanged() {
    final hasText = widget.controller.text.isNotEmpty;
    if (hasText != _hasText) {
      setState(() {
        _hasText = hasText;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.surface,
        borderRadius: BorderRadius.circular(8), // RadiusTokens cohérent
        boxShadow: [
          BoxShadow(
            color: AppColors.shadow,
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Material(
        color: Colors.transparent,
        child: TextField(
          controller: widget.controller,
          enabled: widget.enabled,
          onChanged: widget.onSearch,
          onSubmitted: widget.onSearch,
          decoration: InputDecoration(
            hintText: widget.hintText,
            hintStyle: const TextStyle(
              color: Color(0xFF6B7280),
              fontSize: 14,
            ),
            prefixIcon: const Icon(
              Icons.search,
              color: Color(0xFF6C5CE7), // ColorTokens cohérent
              size: 20,
            ),
            suffixIcon: _hasText
                ? IconButton(
                    onPressed: () {
                      widget.controller.clear();
                      widget.onClear?.call();
                    },
                    icon: const Icon(
                      Icons.clear,
                      color: Color(0xFF6B7280),
                      size: 20,
                    ),
                    tooltip: 'Effacer',
                  )
                : null,
            border: InputBorder.none,
            contentPadding: const EdgeInsets.symmetric(
              horizontal: 16,
              vertical: 12,
            ),
          ),
          style: TextStyle(
            fontSize: 14,
            color: Theme.of(context).colorScheme.onSurface,
          ),
        ),
      ),
    );
  }
}
