// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
// ui_helpers.dart
//
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:nokken/src/features/stats/models/stat_analysis.dart';

/// Helper methods for UI formatting and presentation
class UiHelpers {
  /// Format a date for display
  static String formatDate(DateTime date, {bool includeYear = false}) {
    if (includeYear) {
      return DateFormat('MMM d, yyyy').format(date);
    } else {
      return DateFormat('MMM d').format(date);
    }
  }

  /// Get color for trend type
  static Color getTrendColor(TrendType trendType) {
    switch (trendType) {
      case TrendType.increasing:
        return Colors.green;
      case TrendType.decreasing:
        return Colors.red;
      case TrendType.cyclic:
        return Colors.purple;
      case TrendType.none:
      default:
        return Colors.grey;
    }
  }

  /// Get icon for trend type
  static IconData getTrendIcon(TrendType trendType) {
    switch (trendType) {
      case TrendType.increasing:
        return Icons.trending_up;
      case TrendType.decreasing:
        return Icons.trending_down;
      case TrendType.cyclic:
        return Icons.autorenew;
      case TrendType.none:
      default:
        return Icons.trending_flat;
    }
  }

  /// Format a percentage from a 0-1 ratio
  static String formatPercentage(double ratio, {int decimals = 0}) {
    return '${(ratio * 100).toStringAsFixed(decimals)}%';
  }

  /// Format a score from 0-1 to a human-readable strength description
  static String getStrengthDescription(double score) {
    if (score >= 0.8) {
      return 'Very Strong';
    } else if (score >= 0.6) {
      return 'Strong';
    } else if (score >= 0.4) {
      return 'Moderate';
    } else if (score >= 0.2) {
      return 'Weak';
    } else {
      return 'Very Weak';
    }
  }

  /// Format a factor name for display (remove category prefixes)
  static String formatFactorName(String factorName) {
    if (factorName.startsWith('Hormone: ')) {
      return factorName.substring(9);
    } else if (factorName.startsWith('Emotion: ')) {
      return factorName.substring(9);
    } else if (factorName.startsWith('Activity: ')) {
      return factorName.substring(10);
    } else if (factorName.startsWith('Symptom: ')) {
      return factorName.substring(9);
    }
    return factorName;
  }

  /// Create a gradient color for visualization based on value
  static LinearGradient getValueGradient(double value,
      {bool reversed = false}) {
    final colors = [
      if (!reversed) ...[
        Colors.red,
        Colors.orange,
        Colors.yellow,
        Colors.green,
      ] else ...[
        Colors.green,
        Colors.yellow,
        Colors.orange,
        Colors.red,
      ],
    ];

    return LinearGradient(
      colors: colors,
      stops: const [0.0, 0.33, 0.66, 1.0],
    );
  }

  /// Get confidence level description
  static String getConfidenceDescription(double confidence) {
    if (confidence >= 0.8) {
      return 'very high';
    } else if (confidence >= 0.6) {
      return 'high';
    } else if (confidence >= 0.4) {
      return 'moderate';
    } else if (confidence >= 0.2) {
      return 'low';
    } else {
      return 'very low';
    }
  }
}
