// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
// analysis_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/core/ui/shared_widgets.dart';

/// A card for displaying health analytics data
class AnalysisCard extends StatefulWidget {
  final String title;
  final String? subtitle;
  final Widget content;
  final IconData icon;
  final Color color;
  final VoidCallback? onTap;
  final bool isExpandable;
  final bool initiallyExpanded;
  final VoidCallback? onExpandToggle;

  const AnalysisCard({
    super.key,
    required this.title,
    this.subtitle,
    required this.content,
    required this.icon,
    required this.color,
    this.onTap,
    this.isExpandable = false,
    this.initiallyExpanded = false,
    this.onExpandToggle,
  });

  @override
  State<AnalysisCard> createState() => _AnalysisCardState();
}

class _AnalysisCardState extends State<AnalysisCard> {
  late bool _isExpanded;

  @override
  void initState() {
    super.initState();
    _isExpanded = widget.initiallyExpanded;
  }

  @override
  void didUpdateWidget(AnalysisCard oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.initiallyExpanded != widget.initiallyExpanded) {
      _isExpanded = widget.initiallyExpanded;
    }
  }

  void _toggleExpanded() {
    setState(() {
      _isExpanded = !_isExpanded;
    });

    // Notify parent if needed
    if (widget.onExpandToggle != null) {
      widget.onExpandToggle!();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      child: InkWell(
        onTap: widget.onTap,
        borderRadius: BorderRadius.circular(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Card header
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Row(
                children: [
                  Container(
                    padding: const EdgeInsets.all(8),
                    decoration: BoxDecoration(
                      color: widget.color.withAlpha(30),
                      shape: BoxShape.circle,
                    ),
                    child: Icon(
                      widget.icon,
                      color: widget.color,
                      size: 24,
                    ),
                  ),
                  const SizedBox(width: 12),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          widget.title,
                          style: AppTextStyles.titleMedium,
                        ),
                        if (widget.subtitle != null)
                          Text(
                            widget.subtitle!,
                            style: AppTextStyles.bodySmall.copyWith(
                              color: AppColors.onSurfaceVariant,
                            ),
                          ),
                      ],
                    ),
                  ),
                  if (widget.isExpandable)
                    IconButton(
                      icon: Icon(
                        _isExpanded ? Icons.expand_less : Icons.expand_more,
                      ),
                      onPressed: _toggleExpanded,
                    ),
                ],
              ),
            ),

            // Card content
            AnimatedSize(
              duration: const Duration(milliseconds: 200),
              child: Container(
                padding: EdgeInsets.all(
                    widget.isExpandable && !_isExpanded ? 0 : 16),
                height: widget.isExpandable && !_isExpanded ? 0 : null,
                child:
                    widget.isExpandable && !_isExpanded ? null : widget.content,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
