/// Widget: Product Search Bar
///
/// Custom search bar for filtering products.
library;

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:worker/core/constants/ui_constants.dart';
import 'package:worker/features/products/presentation/providers/search_query_provider.dart';
import 'package:worker/generated/l10n/app_localizations.dart';

/// Product Search Bar Widget
///
/// A search input field that updates the search query provider.
/// Includes search icon and clear button.
class ProductSearchBar extends ConsumerStatefulWidget {
  const ProductSearchBar({super.key});

  @override
  ConsumerState<ProductSearchBar> createState() => _ProductSearchBarState();
}

class _ProductSearchBarState extends ConsumerState<ProductSearchBar> {
  late final TextEditingController _controller;
  late final FocusNode _focusNode;
  bool _hasText = false;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
    _focusNode = FocusNode();

    // Listen to text changes to update clear button visibility
    _controller.addListener(() {
      setState(() {
        _hasText = _controller.text.isNotEmpty;
      });
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  void _onSearchChanged(String value) {
    // Update search query provider
    ref.read(searchQueryProvider.notifier).updateQuery(value);
  }

  void _onClearSearch() {
    _controller.clear();
    ref.read(searchQueryProvider.notifier).clear();
    _focusNode.unfocus();
  }

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);
    final colorScheme = Theme.of(context).colorScheme;

    return SizedBox(
      height: InputFieldSpecs.height,
      child: TextField(
        controller: _controller,
        focusNode: _focusNode,
        onChanged: _onSearchChanged,
        decoration: InputDecoration(
          hintText: l10n.searchProducts,
          hintStyle: TextStyle(
            fontSize: InputFieldSpecs.hintFontSize,
            color: colorScheme.onSurfaceVariant,
          ),
          prefixIcon: Icon(
            FontAwesomeIcons.magnifyingGlass,
            color: colorScheme.onSurfaceVariant,
            size: AppIconSize.md,
          ),
          suffixIcon: _hasText
              ? IconButton(
                  icon: Icon(
                    FontAwesomeIcons.xmark,
                    color: colorScheme.onSurfaceVariant,
                    size: AppIconSize.md,
                  ),
                  onPressed: _onClearSearch,
                )
              : null,
          filled: true,
          fillColor: colorScheme.surface,
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(InputFieldSpecs.borderRadius),
            borderSide: BorderSide.none,
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(InputFieldSpecs.borderRadius),
            borderSide: BorderSide.none,
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(InputFieldSpecs.borderRadius),
            borderSide: BorderSide(
              color: colorScheme.primary,
              width: 2.0,
            ),
          ),
          contentPadding: const EdgeInsets.symmetric(
            horizontal: AppSpacing.md,
            vertical: AppSpacing.md,
          ),
        ),
        style: const TextStyle(fontSize: InputFieldSpecs.fontSize),
      ),
    );
  }
}
