import 'package:flutter/material.dart';
import 'package:superport/screens/common/theme_shadcn.dart';
import 'package:superport/screens/common/components/shadcn_components.dart';

/// 통일된 검색바 위젯
/// 
/// 모든 리스트 화면에서 동일한 스타일과 동작을 보장
class UnifiedSearchBar extends StatelessWidget {
  final TextEditingController controller;
  final String placeholder;
  final VoidCallback onSearch;
  final ValueChanged<String>? onChanged;  // 실시간 검색을 위한 콜백
  final VoidCallback? onClear;
  final Widget? suffixButton;  // 검색 버튼 외 추가 버튼
  final List<Widget>? filters; // 필터 위젯들
  final bool showSearchButton;

  const UnifiedSearchBar({
    super.key,
    required this.controller,
    this.placeholder = '검색어를 입력하세요...',
    required this.onSearch,
    this.onChanged,
    this.onClear,
    this.suffixButton,
    this.filters,
    this.showSearchButton = true,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            // 검색 입력 필드
            Expanded(
              child: Container(
                height: 40,
                decoration: BoxDecoration(
                  color: ShadcnTheme.card,
                  borderRadius: BorderRadius.circular(ShadcnTheme.radiusMd),
                  border: Border.all(color: ShadcnTheme.border),
                ),
                child: TextField(
                  controller: controller,
                  onChanged: onChanged,
                  onSubmitted: (_) => onSearch(),
                  decoration: InputDecoration(
                    hintText: placeholder,
                    hintStyle: TextStyle(color: ShadcnTheme.muted),
                    prefixIcon: Icon(Icons.search, color: ShadcnTheme.muted, size: 20),
                    suffixIcon: controller.text.isNotEmpty && onClear != null
                        ? IconButton(
                            icon: Icon(Icons.clear, color: ShadcnTheme.muted, size: 18),
                            onPressed: () {
                              controller.clear();
                              onClear!();
                            },
                            padding: EdgeInsets.zero,
                            constraints: const BoxConstraints(maxHeight: 40, maxWidth: 40),
                          )
                        : null,
                    border: InputBorder.none,
                    contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                  ),
                ),
              ),
            ),

            // 검색 버튼
            if (showSearchButton) ...[
              const SizedBox(width: ShadcnTheme.spacing2),
              SizedBox(
                height: 40,
                child: ShadcnButton(
                  text: '검색',
                  onPressed: onSearch,
                  variant: ShadcnButtonVariant.primary,
                  textColor: ShadcnTheme.primaryForeground,
                  icon: const Icon(Icons.search, size: 16),
                ),
              ),
            ],

            // 추가 버튼 (예: 추가, 필터 등)
            if (suffixButton != null) ...[
              const SizedBox(width: ShadcnTheme.spacing2),
              suffixButton!,
            ],
          ],
        ),

        // 필터 섹션
        if (filters != null && filters!.isNotEmpty) ...[
          const SizedBox(height: ShadcnTheme.spacing3),
          Row(
            children: [
              ...filters!.map((filter) => Padding(
                padding: const EdgeInsets.only(right: ShadcnTheme.spacing2),
                child: filter,
              )),
            ],
          ),
        ],
      ],
    );
  }
}
