import 'package:flutter/material.dart';
import '../halo_tokens.dart';

/// Decorative gradient blob avatar — the "Bestie" abstract avatar (not a face).
///
/// Ported from `mitra_app/figma-bestie/project/screens/primitives.jsx` (HBOrb).
/// The JSX renders a CSS `radial-gradient` plus two soft white highlight blobs
/// stacked over it. In Flutter we approximate with a `Stack`:
///   - base: `Container` w/ `RadialGradient` from top-left
///   - overlay 1: large soft white blob in upper-left (primary specular)
///   - overlay 2: smaller dimmer blob in lower-right (secondary highlight,
///     opposite the JSX which is also lower-right but very faint — keeping
///     the same position for visual parity)
///   - plus an outer drop shadow tinted by the seed's primary color.
///
/// Approximation note: CSS `filter: blur(6px)` on a sibling layer is emulated
/// with low-opacity white circles. It reads as "soft highlight" at a glance
/// without needing `ImageFilter.blur` (which would require ClipOval + BackdropFilter).
class HaloOrb extends StatelessWidget {
  const HaloOrb({
    super.key,
    this.size = 120,
    this.seed = 0,
  });

  /// Diameter in logical pixels.
  final double size;

  /// 0–4 selects a deterministic color pair from the warm palette seeds.
  /// Out-of-range values are folded with modulo.
  final int seed;

  /// Warm-palette seed table — mirrors `HBOrb` colors in primitives.jsx:68–73.
  static const List<List<Color>> _seeds = [
    [HaloTokens.brand, HaloTokens.accent],
    [HaloTokens.brandDark, HaloTokens.lilac],
    [HaloTokens.accent, HaloTokens.brand],
    [HaloTokens.lilac, HaloTokens.brand],
    [HaloTokens.mint, HaloTokens.brand],
  ];

  @override
  Widget build(BuildContext context) {
    final pair = _seeds[seed.abs() % _seeds.length];
    final primary = pair[0];
    final secondary = pair[1];

    return SizedBox(
      width: size,
      height: size,
      child: Stack(
        children: [
          // Base radial gradient + drop shadow.
          Container(
            width: size,
            height: size,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              gradient: RadialGradient(
                center: const Alignment(-0.4, -0.4), // 30% / 30% in JSX
                radius: 0.85,
                colors: [primary, secondary],
                stops: const [0.0, 0.7],
              ),
              boxShadow: [
                BoxShadow(
                  color: primary.withValues(alpha: 0.25),
                  offset: const Offset(0, 8),
                  blurRadius: 24,
                ),
              ],
            ),
          ),
          // Inner shadow approximation — darker rim, bottom-right.
          // Re-used the JSX `inset -8px -8px 20px rgba(0,0,0,0.12)` intent.
          IgnorePointer(
            child: Container(
              width: size,
              height: size,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                gradient: RadialGradient(
                  center: const Alignment(0.6, 0.6),
                  radius: 0.85,
                  colors: [
                    Colors.black.withValues(alpha: 0.0),
                    Colors.black.withValues(alpha: 0.12),
                  ],
                  stops: const [0.7, 1.0],
                ),
              ),
            ),
          ),
          // Top-left specular highlight (larger, brighter).
          Positioned(
            top: size * 0.12,
            left: size * 0.20,
            child: Container(
              width: size * 0.32,
              height: size * 0.24,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.white.withValues(alpha: 0.55),
              ),
            ),
          ),
          // Bottom-right faint glint.
          Positioned(
            bottom: size * 0.14,
            right: size * 0.18,
            child: Container(
              width: size * 0.18,
              height: size * 0.14,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.white.withValues(alpha: 0.25),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
