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

class InsightsOverviewCard extends StatelessWidget {
  final ComprehensiveAnalysis analysis;
  final VoidCallback onTap;

  const InsightsOverviewCard({
    super.key,
    required this.analysis,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    // Get key insights from each category
    final patterns = analysis.patterns.take(1).toList();
    final relationships = analysis.relationships.take(1).toList();
    final factorRankings = analysis.factorRankings.take(1).toList();
    final anomalies = analysis.anomalies.take(1).toList();

    return Card(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      elevation: 2,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(16),
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Header
              Row(
                children: [
                  Container(
                    padding: const EdgeInsets.all(8),
                    decoration: BoxDecoration(
                      color: Colors.purple.withAlpha(30),
                      shape: BoxShape.circle,
                    ),
                    child: const Icon(
                      Icons.psychology,
                      color: Colors.purple,
                      size: 24,
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'Health Insights',
                          style: AppTextStyles.titleMedium,
                        ),
                        Text(
                          'Patterns, correlations & key factors',
                          style: AppTextStyles.bodySmall.copyWith(
                            color: AppColors.onSurfaceVariant,
                          ),
                        ),
                      ],
                    ),
                  ),
                  const Icon(Icons.arrow_forward_ios, size: 16),
                ],
              ),
              const SizedBox(height: 16),

              // Preview of insights
              // Pattern insight
              if (patterns.isNotEmpty)
                _buildInsightPreview(
                  title: 'Pattern detected',
                  description: patterns.first.name,
                  icon: StatUtils.getPatternIcon(patterns.first.type),
                  color: patterns.first.patternColor,
                ),

              // Relationship insight
              if (relationships.isNotEmpty)
                _buildInsightPreview(
                  title: 'Strong correlation',
                  description: relationships.first.factorNames.join(' & '),
                  icon: Icons.compare_arrows,
                  color: Colors.blue,
                ),

              // Factor ranking insight
              if (factorRankings.isNotEmpty)
                _buildInsightPreview(
                  title: 'Key factor identified',
                  description: 'Impact on ${factorRankings.first.targetFactor}',
                  icon: Icons.insights,
                  color: Colors.teal,
                ),

              // Anomaly insight
              if (anomalies.isNotEmpty)
                _buildInsightPreview(
                  title: 'Unusual data point',
                  description:
                      '${anomalies.first.factorName} on ${anomalies.first.formattedDate}',
                  icon: Icons.warning,
                  color: Colors.orange,
                ),

              // No insights case
              if (patterns.isEmpty &&
                  relationships.isEmpty &&
                  factorRankings.isEmpty &&
                  anomalies.isEmpty)
                _buildInsightPreview(
                  title: 'Keep tracking',
                  description: 'Continue tracking to reveal insights',
                  icon: Icons.info_outline,
                  color: Colors.grey,
                ),

              const SizedBox(height: 8),

              Center(
                child: Text(
                  'Tap to explore all insights',
                  style: AppTextStyles.bodySmall.copyWith(
                    fontStyle: FontStyle.italic,
                    color: AppColors.onSurfaceVariant,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildInsightPreview({
    required String title,
    required String description,
    required IconData icon,
    required Color color,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8.0),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(6),
            decoration: BoxDecoration(
              color: color.withAlpha(20),
              shape: BoxShape.circle,
            ),
            child: Icon(
              icon,
              color: color,
              size: 14,
            ),
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: AppTextStyles.bodySmall.copyWith(
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  description,
                  style: AppTextStyles.bodySmall,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
