import 'package:flutter/material.dart';
import 'package:frontend_eccp_mobile/app/core/constants/constants.dart';

class AppAccordion extends StatefulWidget {
  const AppAccordion({
    required this.title,
    required this.child,
    this.description,
    this.initialExpanded = false,
    super.key,
  });

  final String title;
  final String? description;
  final Widget child;
  final bool initialExpanded;

  @override
  State<AppAccordion> createState() => _AppAccordionState();
}

class _AppAccordionState extends State<AppAccordion> {
  late bool expanded;

  @override
  void initState() {
    super.initState();
    expanded = widget.initialExpanded;
  }

  void toggle() {
    setState(() {
      expanded = !expanded;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: AppPadding.b12,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        border: Border.all(color: AppColors.neutral200),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.06),
            blurRadius: 3,
          ),
        ],
      ),
      child: Column(
        children: [
          InkWell(
            onTap: toggle,
            child: Padding(
              padding: const EdgeInsets.symmetric(
                horizontal: 14,
                vertical: 12,
              ),
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      widget.title,
                      style: AppTextStyles.label.copyWith(
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                  ),
                  AnimatedRotation(
                    turns: expanded ? 0.5 : 0,
                    duration: AppDurations.fast,
                    child: Icon(
                      Icons.keyboard_arrow_down,
                      color: AppColors.neutral400,
                    ),
                  ),
                ],
              ),
            ),
          ),
          ClipRect(
            child: AnimatedAlign(
              alignment: Alignment.topCenter,
              heightFactor: expanded ? 1 : 0,
              duration: AppDurations.fast,
              curve: Curves.easeInOut,
              child: AnimatedOpacity(
                opacity: expanded ? 1 : 0,
                duration: AppDurations.fast,
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(14, 0, 14, 12),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (widget.description != null) ...[
                        Text(
                          widget.description!,
                          style: AppTextStyles.small.copyWith(
                            fontStyle: FontStyle.italic,
                            color: AppColors.neutral700,
                          ),
                        ),
                        AppSpacing.h8,
                      ],
                      widget.child,
                    ],
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
