import 'package:flutter/material.dart';
import 'package:kasy_kit/core/theme/theme.dart';

/// Optional visual style for [KasyChip].
enum KasyChipVariant {
  /// Neutral surface — [AssistChip].
  assist,
  /// Tonal / brand hint — [AssistChip] with tonal styling when available.
  tonal,
}

/// Compact action or filter chip on top of Material 3 [ActionChip] / [FilterChip].
class KasyChip extends StatelessWidget {
  final String label;
  /// When non-null, builds a [FilterChip] (toggle). Otherwise an [ActionChip].
  final bool selected;
  final ValueChanged<bool>? onSelected;
  final VoidCallback? onPressed;
  final KasyChipVariant variant;
  final IconData? icon;
  final bool enabled;

  const KasyChip({
    super.key,
    required this.label,
    this.selected = false,
    this.onSelected,
    this.onPressed,
    this.variant = KasyChipVariant.assist,
    this.icon,
    this.enabled = true,
  });

  @override
  Widget build(BuildContext context) {
    final KasyColors c = context.colors;
    final Widget? avatar = icon == null
        ? null
        : Icon(icon, size: 18, color: enabled ? c.primary : c.muted);
    final OutlinedBorder chipShape = RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(KasyRadius.full),
      side: BorderSide(color: c.outline.withValues(alpha: 0.45)),
    );
    final Color surfaceTint = variant == KasyChipVariant.tonal
        ? c.primary.withValues(alpha: 0.12)
        : c.surface;
    if (onSelected != null) {
      return FilterChip(
        label: Text(label),
        selected: selected,
        onSelected: enabled ? onSelected : null,
        avatar: avatar,
        showCheckmark: false,
        shape: chipShape,
        selectedColor: c.primary.withValues(alpha: 0.18),
        backgroundColor: surfaceTint,
        disabledColor: c.outline.withValues(alpha: 0.2),
        labelStyle: context.textTheme.labelLarge?.copyWith(
          color: enabled ? c.onSurface : c.muted,
        ),
        padding: const EdgeInsets.symmetric(
          horizontal: KasySpacing.sm,
          vertical: KasySpacing.xs,
        ),
      );
    }
    return ActionChip(
      label: Text(label),
      onPressed: enabled ? onPressed : null,
      avatar: avatar,
      shape: chipShape,
      backgroundColor: surfaceTint,
      disabledColor: c.outline.withValues(alpha: 0.2),
      labelStyle: context.textTheme.labelLarge?.copyWith(
        color: enabled ? c.onSurface : c.muted,
      ),
      padding: const EdgeInsets.symmetric(
        horizontal: KasySpacing.sm,
        vertical: KasySpacing.xs,
      ),
    );
  }
}
