// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
//  hormone_search_bar.dart
//  Reusable search bar for hormone filtering
//
import 'package:flutter/material.dart';
import 'package:nokken/src/core/ui/theme/app_colors.dart';
import 'package:nokken/src/core/ui/theme/app_text_styles.dart';

class HormoneSearchBar extends StatefulWidget {
  final String hintText;
  final Function(String) onSearch;
  final bool autofocus;
  final bool inAppBar;

  const HormoneSearchBar({
    super.key,
    this.hintText = 'Search hormones...',
    required this.onSearch,
    this.autofocus = false,
    this.inAppBar = false,
  });

  @override
  State<HormoneSearchBar> createState() => _HormoneSearchBarState();
}

class _HormoneSearchBarState extends State<HormoneSearchBar> {
  final TextEditingController _controller = TextEditingController();

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

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: widget.inAppBar
          ? EdgeInsets.zero
          : const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: TextField(
        controller: _controller,
        autofocus: widget.autofocus,
        decoration: InputDecoration(
          hintText: widget.hintText,
          isDense: true,
          contentPadding: const EdgeInsets.symmetric(
            horizontal: 12,
            vertical: 12,
          ),
          prefixIcon: Icon(Icons.search, color: AppColors.primary),
          suffixIcon: _controller.text.isNotEmpty
              ? IconButton(
                  icon: Icon(Icons.clear, color: AppColors.primary),
                  onPressed: () {
                    _controller.clear();
                    widget.onSearch('');
                  },
                )
              : null,
          filled: true,
          fillColor: AppColors.surface,
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(widget.inAppBar ? 20 : 20),
            borderSide: BorderSide(color: AppColors.outline),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(widget.inAppBar ? 20 : 20),
            borderSide: BorderSide(color: AppColors.outline),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(widget.inAppBar ? 20 : 20),
            borderSide: BorderSide(color: AppColors.primary, width: 2),
          ),
        ),
        style: AppTextStyles.bodyMedium,
        onChanged: widget.onSearch,
      ),
    );
  }
}
