// ChainPill — small inline label. Used for capabilities, versions,
// permission scopes, status text. Replaces ad-hoc Container chips.

import 'package:flutter/material.dart';

import '../theme/theme.dart';
import '../theme/tokens.dart';

enum ChainPillTone { neutral, accent, success, warning, danger }

class ChainPill extends StatelessWidget {
  final String label;
  final ChainPillTone tone;
  final IconData? icon;
  final bool monospace;

  const ChainPill({
    super.key,
    required this.label,
    this.tone = ChainPillTone.neutral,
    this.icon,
    this.monospace = false,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final (bg, fg) = _colors(theme.colorScheme);
    final textStyle = monospace
        ? ChainTheme.mono(size: 11, weight: FontWeight.w500, color: fg)
        : theme.textTheme.labelSmall?.copyWith(color: fg);
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: ChainSpace.sm, vertical: 3),
      decoration: BoxDecoration(
        color: bg,
        borderRadius: BorderRadius.circular(ChainRadius.sm),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (icon != null) ...[
            Icon(icon, size: 11, color: fg),
            const SizedBox(width: 4),
          ],
          Text(label, style: textStyle),
        ],
      ),
    );
  }

  (Color, Color) _colors(ColorScheme scheme) {
    switch (tone) {
      case ChainPillTone.neutral:
        return (scheme.surfaceContainerHighest, scheme.onSurfaceVariant);
      case ChainPillTone.accent:
        return (scheme.primary.withValues(alpha: 0.15), scheme.primary);
      case ChainPillTone.success:
        return (
          ChainColors.success.withValues(alpha: 0.15),
          scheme.brightness == Brightness.dark
              ? ChainColors.success
              : const Color(0xFF15803D),
        );
      case ChainPillTone.warning:
        return (
          ChainColors.warning.withValues(alpha: 0.15),
          scheme.brightness == Brightness.dark
              ? ChainColors.warning
              : const Color(0xFFB45309),
        );
      case ChainPillTone.danger:
        return (scheme.errorContainer, scheme.onErrorContainer);
    }
  }
}
