import 'package:flutter/material.dart';

import '../../../core/ui/product_picker_field.dart';
import '../domain/admin_product.dart';

enum ProductScopeFilter { all, globalOnly, privateOnly }

List<String> buildLocationOptionsFromValues(Iterable<String?> values) {
  final set = <String>{};
  for (final value in values) {
    final trimmed = value?.trim();
    if (trimmed != null && trimmed.isNotEmpty) {
      set.add(trimmed);
    }
  }
  final list = set.toList()
    ..sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
  return list;
}

String? resolveLocationDropdownValue({
  required bool useManualLocation,
  required String currentValue,
  required List<String> options,
  required String manualLocationValue,
}) {
  if (useManualLocation) return manualLocationValue;
  final value = currentValue.trim();
  if (value.isEmpty) return null;
  return options.contains(value) ? value : manualLocationValue;
}

List<AdminProduct> filterSelectableAdminProducts({
  required List<AdminProduct> products,
  required int? ownerUserId,
  required int? categoryId,
  required ProductScopeFilter scopeFilter,
  required AdminProduct? selectedProduct,
}) {
  final ownerFiltered = ownerUserId == null
      ? products.where((p) => p.isPrivate != true).toList()
      : products
          .where(
            (p) => p.isPrivate != true || p.ownerId == ownerUserId,
          )
          .toList();

  final scopeFiltered = switch (scopeFilter) {
    ProductScopeFilter.all => ownerFiltered,
    ProductScopeFilter.globalOnly => ownerFiltered.where((p) => p.isPrivate != true).toList(),
    ProductScopeFilter.privateOnly => ownerFiltered.where((p) => p.isPrivate == true).toList(),
  };

  final source = categoryId == null
      ? scopeFiltered
      : scopeFiltered.where((p) => p.categoryId == categoryId).toList();

  if (selectedProduct != null && !source.any((p) => p.id == selectedProduct.id)) {
    source.add(selectedProduct);
  }

  source.sort((a, b) => a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase()));
  return source;
}

List<ProductOption> toProductOptions(List<AdminProduct> products) {
  return products
      .map(
        (p) => (
          id: p.id,
          name: p.isPrivate == true ? '${p.displayName} (privat)' : p.displayName,
          categoryId: p.categoryId,
        ),
      )
      .toList();
}

Widget buildCategoryPathChip(String? categoryPath, {double maxWidth = 220}) {
  if (categoryPath == null || categoryPath.trim().isEmpty) {
    return const SizedBox.shrink();
  }
  final value = categoryPath.trim();
  return Tooltip(
    message: value,
    child: Chip(
      label: ConstrainedBox(
        constraints: BoxConstraints(maxWidth: maxWidth),
        child: Text(
          value,
          overflow: TextOverflow.ellipsis,
          softWrap: false,
          style: const TextStyle(fontSize: 12),
        ),
      ),
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
    ),
  );
}
