// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
// pattern_explanation_widget.dart
//
import 'package:flutter/material.dart';
import 'package:nokken/src/core/ui/theme/app_text_styles.dart';
import 'package:nokken/src/core/ui/widgets/shared_widgets.dart';
import 'package:nokken/src/features/stats/models/stat_analysis.dart';
import 'package:nokken/src/features/stats/utils/stat_utils.dart';

class PatternExplanationWidget extends StatelessWidget {
  const PatternExplanationWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Pattern Types:',
          style: AppTextStyles.bodyMedium.copyWith(fontWeight: FontWeight.bold),
        ),
        SharedWidgets.verticalSpace(12),
        _buildPatternExplanation(
          PatternType.trend,
          Colors.blue,
          'Trend patterns show steady increases or decreases in a health factor over time.',
        ),
        SharedWidgets.verticalSpace(8),
        _buildPatternExplanation(
          PatternType.cyclical,
          Colors.purple,
          'Cyclical patterns show regular fluctuations that repeat over time (e.g., weekly or monthly cycles).',
        ),
        SharedWidgets.verticalSpace(8),
        _buildPatternExplanation(
          PatternType.cluster,
          Colors.teal,
          'Cluster patterns reveal groups of related factors that tend to change together.',
        ),
        SharedWidgets.verticalSpace(8),
        _buildPatternExplanation(
          PatternType.threshold,
          Colors.orange,
          'Threshold patterns identify when values consistently cross important boundaries.',
        ),
      ],
    );
  }

  Widget _buildPatternExplanation(
      PatternType type, Color color, String explanation) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: const EdgeInsets.all(8),
          decoration: BoxDecoration(
            color: color.withAlpha(20),
            borderRadius: BorderRadius.circular(8),
          ),
          child: Icon(
            StatUtils.getPatternIcon(type),
            size: 20,
            color: color,
          ),
        ),
        SharedWidgets.horizontalSpace(12),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                type.name,
                style: AppTextStyles.bodyMedium.copyWith(
                  fontWeight: FontWeight.bold,
                  color: color,
                ),
              ),
              Text(
                explanation,
                style: AppTextStyles.bodySmall,
              ),
            ],
          ),
        ),
      ],
    );
  }
}

/// Helper widget for pattern type chips
class PatternTypeChip extends StatelessWidget {
  final IconData icon;
  final String label;
  final Color color;

  const PatternTypeChip({
    super.key,
    required this.icon,
    required this.label,
    required this.color,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: color.withAlpha(20),
        borderRadius: BorderRadius.circular(16),
        border: Border.all(
          color: color,
          width: 1,
        ),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            icon,
            size: 16,
            color: color,
          ),
          SharedWidgets.horizontalSpace(4),
          Text(
            label,
            style: AppTextStyles.bodySmall.copyWith(
              color: color,
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }
}
