import 'package:flutter/material.dart';

import '../../../core/ui/product_picker_field.dart' as product_picker;
import '../../../core/ui/searchable_category_field.dart';

class InventoryCategoryProductSection extends StatelessWidget {
  final List<CategorySelectOption> categoryOptions;
  final int? selectedCategoryId;
  final ValueChanged<String?> onCategoryChanged;
  final String categorySearchLabel;
  final VoidCallback onPickCategory;
  final String pickCategoryLabel;
  final VoidCallback onClearCategory;
  final String clearCategoryLabel;
  final bool canPickCategory;
  final bool canClearCategory;
  final List<product_picker.ProductOption> productOptions;
  final int? selectedProductId;
  final ValueChanged<int?> onProductChanged;
  final Future<product_picker.ProductOption?> Function(String name)? onCreateProduct;
  final bool isLoadingProducts;
  final bool productEnabled;
  final String productLabel;

  const InventoryCategoryProductSection({
    super.key,
    required this.categoryOptions,
    required this.selectedCategoryId,
    required this.onCategoryChanged,
    required this.categorySearchLabel,
    required this.onPickCategory,
    required this.pickCategoryLabel,
    required this.onClearCategory,
    required this.clearCategoryLabel,
    required this.canPickCategory,
    required this.canClearCategory,
    required this.productOptions,
    required this.selectedProductId,
    required this.onProductChanged,
    this.onCreateProduct,
    required this.isLoadingProducts,
    required this.productEnabled,
    required this.productLabel,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SearchableCategoryField(
          options: categoryOptions,
          value: selectedCategoryId?.toString(),
          label: categorySearchLabel,
          onChanged: onCategoryChanged,
        ),
        const SizedBox(height: 12),
        Wrap(
          spacing: 8,
          runSpacing: 8,
          children: [
            OutlinedButton.icon(
              onPressed: canPickCategory ? onPickCategory : null,
              icon: const Icon(Icons.category_outlined),
              label: Text(pickCategoryLabel),
            ),
            OutlinedButton.icon(
              onPressed: canClearCategory ? onClearCategory : null,
              icon: const Icon(Icons.clear),
              label: Text(clearCategoryLabel),
            ),
          ],
        ),
        const SizedBox(height: 12),
        product_picker.ProductPickerField(
          products: productOptions,
          value: selectedProductId,
          isLoading: isLoadingProducts,
          enabled: productEnabled,
          label: productLabel,
          onChanged: onProductChanged,
          onCreate: onCreateProduct,
        ),
      ],
    );
  }
}
