import 'package:flutter/material.dart';

import '../model/dropdown_option.dart';
import '../model/enquiry_model.dart';
import 'enquiry_row.dart';

class EnquiryGroup extends StatefulWidget {
  final String title;
  final String icon;
  final String groupKey;
  final List<EnquiryModel> enquiries;
  final Function(EnquiryModel) onUpdate;
  final List<DropdownOption> partners;
  final List<DropdownOption> insurers;
  final List<DropdownOption> assignees;

  const EnquiryGroup({
    super.key,
    required this.title,
    required this.icon,
    required this.groupKey,
    required this.enquiries,
    required this.onUpdate,
    required this.partners,
    required this.insurers,
    required this.assignees,
  });

  @override
  State<EnquiryGroup> createState() => _EnquiryGroupState();
}

class _EnquiryGroupState extends State<EnquiryGroup> {
  bool _isExpanded = true;

  @override
  Widget build(BuildContext context) {
    return SelectionArea(
        child:Column(
      children: [
        InkWell(
          onTap: () => setState(() => _isExpanded = !_isExpanded),
          child: Container(
            color: const Color(0xFFf8f9fa),
            padding: const EdgeInsets.all(12),
            child: Row(
              children: [
                Icon(
                  _isExpanded ? Icons.arrow_drop_down : Icons.arrow_right,
                  size: 24,
                ),
                const SizedBox(width: 8),
                Text(
                  '${widget.icon} ${widget.title}',
                  style: const TextStyle(fontWeight: FontWeight.w600),
                ),
                const Spacer(),
                Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 10,
                    vertical: 2,
                  ),
                  decoration: BoxDecoration(
                    color: const Color(0xFFe3f2fd),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Text(
                    '${widget.enquiries.length} Records',
                    style: const TextStyle(
                      fontSize: 11,
                      color: Color(0xFF1976d2),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        if (_isExpanded)
          ...widget.enquiries.isEmpty
              ? [
                  const Padding(
                    padding: EdgeInsets.all(30),
                    child: Text(
                      'No records found',
                      style: TextStyle(
                        color: Colors.grey,
                        fontStyle: FontStyle.italic,
                      ),
                    ),
                  ),
                ]
              : widget.enquiries.map((enquiry) {
                  return EnquiryRow(
                    enquiry: enquiry,
                    onUpdate: widget.onUpdate,
                    partners: widget.partners,
                    insurers: widget.insurers,
                    assignees: widget.assignees,
                  );
                }).toList(),
      ],
    ));
  }
}
