import 'package:flutter/material.dart';

/// Kasy Design System — Color Tokens
///
/// Single source of truth for all colors in the app.
/// To change a color across the entire app, change it here.
///
/// Usage via context extension:
///   context.colors.primary
///   context.colors.surfacePrimarySoft / .surfaceNeutralSoft / .surfaceErrorSoft
///   context.colors.outlineButton
///   context.colors.success / .muted
class KasyColors extends ThemeExtension<KasyColors> {
  /// Vermelho de erro/danger — mesmo tom claro/escuro; vivo e saturado (#FF3B30, estilo sistema).
  static const Color semanticError = Color(0xFFFF3B30);

  // --- Brand ---
  final Color primary;
  final Color onPrimary;

  // --- Backgrounds ---
  final Color background;
  final Color onBackground;

  // --- Surfaces ---
  final Color surface;
  final Color onSurface;

  /// Filled tint behind primary-accent controls (pill “secondary”: pale blue /
  /// light; charcoal tint / dark). Foreground stays [primary].
  final Color surfacePrimarySoft;

  /// Filled neutral tint (“tertiary” row: light grey / light; charcoal / dark).
  /// Foreground: [onSurface].
  final Color surfaceNeutralSoft;

  /// Destructive soft fill (“danger soft”: pale rose / light; saturated burgundy
  /// / dark). Foreground stays [error].
  final Color surfaceErrorSoft;

  /// Neutral fill for avatar icon/initials fallback and stacked “+N” (theme-aware).
  final Color avatarFallbackFill;

  // --- Semantic: Error ---
  final Color error;
  final Color onError;

  // --- Semantic: Success ---
  final Color success;
  final Color onSuccess;

  // --- Semantic: Warning ---
  final Color warning;
  final Color onWarning;

  // --- Structural ---
  /// Default divider / input borders.
  final Color outline;
  /// Pills/chips/outline buttons on canvas (readable in dark mode).
  final Color outlineButton;
  /// Secondary text / disabled state.
  final Color muted;

  /// Form field labels ([KasyTextField], [KasyTextArea]): slightly darker than
  /// [muted] in light mode, slightly lighter in dark mode.
  Color get fieldLabel {
    final bool isDark = background.computeLuminance() < 0.5;
    return Color.lerp(muted, onSurface, isDark ? 0.36 : 0.28)!;
  }

  // --- Greys (legacy — prefer muted/outline for new code) ---
  final Color grey1;
  final Color grey2;
  final Color grey3;

  // --- Premium (paywall visual identity) ---
  final Color premiumOverlayMid;   // paywall background gradient — mid tone
  final Color premiumOverlayDark;  // paywall background gradient — dark tone
  final Color premiumBannerText;   // text color for the premium banner (PremiumWideBanner)

  const KasyColors({
    required this.primary,
    required this.onPrimary,
    required this.background,
    required this.onBackground,
    required this.surface,
    required this.onSurface,
    required this.surfacePrimarySoft,
    required this.surfaceNeutralSoft,
    required this.surfaceErrorSoft,
    required this.avatarFallbackFill,
    required this.error,
    required this.onError,
    required this.success,
    required this.onSuccess,
    required this.warning,
    required this.onWarning,
    required this.outline,
    required this.outlineButton,
    required this.muted,
    required this.grey1,
    required this.grey2,
    required this.grey3,
    required this.premiumOverlayMid,
    required this.premiumOverlayDark,
    required this.premiumBannerText,
  });

  factory KasyColors.light() => const KasyColors(
        primary: Color(0xFF1E88E5),
        onPrimary: Color(0xFFFFFFFF),
        // Canvas: slightly lighter than systemGroupedBackground for a softer wash; surfaces stay white.
        background: Color(0xFFF7F7F7),
        onBackground: Color(0xFF000000),
        surface: Color(0xFFFFFFFF),
        onSurface: Color(0xFF000000),
        surfacePrimarySoft: Color(0xFFDFE6F0),
        surfaceNeutralSoft: Color(0xFFEFEFEF),
        surfaceErrorSoft: Color(0xFFFFEBEE),
        avatarFallbackFill: Color(0xFFF2F2F2),
        error: KasyColors.semanticError,
        onError: Color(0xFFFFFFFF),
        success: Color(0xFF34C759),
        onSuccess: Color(0xFFFFFFFF),
        warning: Color(0xFFF57F17),
        onWarning: Color(0xFFFFFFFF),
        outline: Color(0xFFC6C6C8),
        outlineButton: Color(0xFFB8BFC9),
        muted: Color(0xFF7D8488),
        grey1: Color(0xFFBEC1C3),
        grey2: Color(0xFF7D8488),
        grey3: Color(0xFF263238),
        premiumOverlayMid: Color(0xFF9997A1),
        premiumOverlayDark: Color(0xFF130F26),
        premiumBannerText: Color(0xFF49250A),
      );

  factory KasyColors.dark() => const KasyColors(
        primary: Color(0xFF1E88E5),
        onPrimary: Color(0xFFFFFFFF),
        // Canvas: Material dark baseline; surfaces: iOS-style elevated grey (cards / chrome).
        background: Color(0xFF060608),
        onBackground: Color(0xFFFAFAFA),
        surface: Color(0xFF18181B),
        onSurface: Color(0xFFFFFFFF),
        surfacePrimarySoft: Color(0xFF2C323A),
        surfaceNeutralSoft: Color(0xFF232325),
        surfaceErrorSoft: Color(0xFF3F2A2D),
        avatarFallbackFill: Color(0xFF2C2C2E),
        error: KasyColors.semanticError,
        onError: Color(0xFFFFFFFF),
        success: Color(0xFF30D158),
        onSuccess: Color(0xFFFFFFFF),
        warning: Color(0xFFFFCA28),
        onWarning: Color(0xFF000000),
        outline: Color(0xFF38383A),
        outlineButton: Color(0x4DFFFFFF),
        muted: Color(0xFF7D8488),
        grey1: Color(0xFF263238),
        grey2: Color(0xFF7D8488),
        grey3: Color(0xFFBEC1C3),
        premiumOverlayMid: Color(0xFF9997A1),
        premiumOverlayDark: Color(0xFF130F26),
        premiumBannerText: Color(0xFF49250A),
      );

  @override
  ThemeExtension<KasyColors> copyWith({
    Color? primary,
    Color? onPrimary,
    Color? background,
    Color? onBackground,
    Color? surface,
    Color? onSurface,
    Color? surfacePrimarySoft,
    Color? surfaceNeutralSoft,
    Color? surfaceErrorSoft,
    Color? avatarFallbackFill,
    Color? error,
    Color? onError,
    Color? success,
    Color? onSuccess,
    Color? warning,
    Color? onWarning,
    Color? outline,
    Color? outlineButton,
    Color? muted,
    Color? grey1,
    Color? grey2,
    Color? grey3,
    Color? premiumOverlayMid,
    Color? premiumOverlayDark,
    Color? premiumBannerText,
  }) {
    return KasyColors(
      primary: primary ?? this.primary,
      onPrimary: onPrimary ?? this.onPrimary,
      background: background ?? this.background,
      onBackground: onBackground ?? this.onBackground,
      surface: surface ?? this.surface,
      onSurface: onSurface ?? this.onSurface,
      surfacePrimarySoft: surfacePrimarySoft ?? this.surfacePrimarySoft,
      surfaceNeutralSoft: surfaceNeutralSoft ?? this.surfaceNeutralSoft,
      surfaceErrorSoft: surfaceErrorSoft ?? this.surfaceErrorSoft,
      avatarFallbackFill: avatarFallbackFill ?? this.avatarFallbackFill,
      error: error ?? this.error,
      onError: onError ?? this.onError,
      success: success ?? this.success,
      onSuccess: onSuccess ?? this.onSuccess,
      warning: warning ?? this.warning,
      onWarning: onWarning ?? this.onWarning,
      outline: outline ?? this.outline,
      outlineButton: outlineButton ?? this.outlineButton,
      muted: muted ?? this.muted,
      grey1: grey1 ?? this.grey1,
      grey2: grey2 ?? this.grey2,
      grey3: grey3 ?? this.grey3,
      premiumOverlayMid: premiumOverlayMid ?? this.premiumOverlayMid,
      premiumOverlayDark: premiumOverlayDark ?? this.premiumOverlayDark,
      premiumBannerText: premiumBannerText ?? this.premiumBannerText,
    );
  }

  @override
  ThemeExtension<KasyColors> lerp(
    covariant ThemeExtension<KasyColors>? other,
    double t,
  ) {
    if (other == null || other is! KasyColors) return this;

    return KasyColors(
      primary: Color.lerp(primary, other.primary, t)!,
      onPrimary: Color.lerp(onPrimary, other.onPrimary, t)!,
      background: Color.lerp(background, other.background, t)!,
      onBackground: Color.lerp(onBackground, other.onBackground, t)!,
      surface: Color.lerp(surface, other.surface, t)!,
      onSurface: Color.lerp(onSurface, other.onSurface, t)!,
      surfacePrimarySoft:
          Color.lerp(surfacePrimarySoft, other.surfacePrimarySoft, t)!,
      surfaceNeutralSoft:
          Color.lerp(surfaceNeutralSoft, other.surfaceNeutralSoft, t)!,
      surfaceErrorSoft: Color.lerp(surfaceErrorSoft, other.surfaceErrorSoft, t)!,
      avatarFallbackFill:
          Color.lerp(avatarFallbackFill, other.avatarFallbackFill, t)!,
      error: Color.lerp(error, other.error, t)!,
      onError: Color.lerp(onError, other.onError, t)!,
      success: Color.lerp(success, other.success, t)!,
      onSuccess: Color.lerp(onSuccess, other.onSuccess, t)!,
      warning: Color.lerp(warning, other.warning, t)!,
      onWarning: Color.lerp(onWarning, other.onWarning, t)!,
      outline: Color.lerp(outline, other.outline, t)!,
      outlineButton: Color.lerp(outlineButton, other.outlineButton, t)!,
      muted: Color.lerp(muted, other.muted, t)!,
      grey1: Color.lerp(grey1, other.grey1, t)!,
      grey2: Color.lerp(grey2, other.grey2, t)!,
      grey3: Color.lerp(grey3, other.grey3, t)!,
      premiumOverlayMid: Color.lerp(premiumOverlayMid, other.premiumOverlayMid, t)!,
      premiumOverlayDark: Color.lerp(premiumOverlayDark, other.premiumOverlayDark, t)!,
      premiumBannerText: Color.lerp(premiumBannerText, other.premiumBannerText, t)!,
    );
  }
}
