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

/// Visual surface treatment for [KasyCard].
enum KasyCardVariant {
  /// White/surface background, subtle shadow, hairline border.
  elevated,

  /// Neutral fill ([surfaceNeutralSoft]), no shadow.
  filled,

  /// Transparent background, hairline border only.
  outlined,

  /// No background, no border, no shadow.
  ghost,
}

/// Design-system surface card.
///
/// [variant] controls background, border, and shadow. Use [onTap] for
/// interactive cards (adds [KasyPressableDepth] feedback).
///
/// [borderColor] overrides the variant's default border color. Useful for
/// tinted flat cards (e.g. combine with [KasyCardVariant.outlined] and [color]
/// to get a shadow-free card with a custom border tint).
class KasyCard extends StatelessWidget {
  final Widget child;
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final Color? color;
  final Color? borderColor;
  final bool clipContent;
  final KasyCardVariant variant;
  final BorderRadius? borderRadius;
  final VoidCallback? onTap;
  final String? semanticLabel;

  const KasyCard({
    super.key,
    required this.child,
    this.margin,
    this.padding,
    this.color,
    this.borderColor,
    this.clipContent = true,
    this.variant = KasyCardVariant.elevated,
    this.borderRadius,
    this.onTap,
    this.semanticLabel,
  });

  @override
  Widget build(BuildContext context) {
    final KasyColors c = context.colors;
    final BorderRadius resolvedRadius =
        borderRadius ?? BorderRadius.circular(KasyRadius.xl);

    final Color bg;
    final Border? border;
    final List<BoxShadow>? shadows;

    final BoxShadow tightShadow = KasyShadows.component(
      context,
      blurRadius: 3.0,
      spreadRadius: -2.0,
      offset: const Offset(0, 0.8),
    );

    final BoxShadow subtleShadow = KasyShadows.component(
      context,
      blurRadius: 2.0,
      spreadRadius: -2.0,
      offset: const Offset(0, 0.5),
    );

    final bool dark = context.isDark;

    switch (variant) {
      case KasyCardVariant.elevated:
        bg = color ?? c.surface;
        border = dark ? null : Border.all(color: c.outline.withValues(alpha: 0.18));
        shadows = <BoxShadow>[tightShadow];
      case KasyCardVariant.filled:
        bg = color ?? c.surfaceNeutralSoft;
        border = null;
        shadows = <BoxShadow>[subtleShadow];
      case KasyCardVariant.outlined:
        bg = color ?? Colors.transparent;
        border = Border.all(color: c.outline.withValues(alpha: dark ? 0.48 : 0.38));
        shadows = null;
      case KasyCardVariant.ghost:
        bg = color ?? Colors.transparent;
        border = null;
        shadows = null;
    }

    final Widget content =
        padding == null ? child : Padding(padding: padding!, child: child);

    final Widget card = DecoratedBox(
      decoration: BoxDecoration(
        color: bg,
        borderRadius: resolvedRadius,
        border: borderColor != null ? Border.all(color: borderColor!) : border,
        boxShadow: shadows,
      ),
      child: clipContent
          ? ClipRRect(borderRadius: resolvedRadius, child: content)
          : content,
    );

    final Widget margined =
        margin != null ? Padding(padding: margin!, child: card) : card;

    if (onTap != null) {
      return KasyPressableDepth(
        onPressed: onTap!,
        semanticLabel: semanticLabel ?? 'Card',
        clipBorderRadius: resolvedRadius,
        child: margined,
      );
    }

    return margined;
  }
}
