import 'package:flutter/material.dart';

import '../widgets/marketing_performance_chart.dart';
import '../widgets/marketing_ui.dart';
import 'marketing_page_common.dart';

class MarketingMonthlyPerformancePage extends StatelessWidget {
  const MarketingMonthlyPerformancePage({super.key, required this.records, required this.onAdd});
  final List<Map<String, dynamic>> records;
  final VoidCallback onAdd;

  String _month(dynamic value) {
    const months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    final index = int.tryParse('$value') ?? 0;
    return index > 0 && index < months.length ? months[index] : 'Month';
  }

  @override
  Widget build(BuildContext context) => MarketingPageFrame(
        title: 'Monthly Performance',
        subtitle: 'Capture and compare the monthly recruitment indicators for each center',
        icon: Icons.track_changes_outlined,
        actionText: 'Enter indicators',
        onAction: onAdd,
        children: [
          if (records.isEmpty)
            const MarketingEmptyState(text: 'No monthly figures yet. Enter indicators for a service center.')
          else ...[
            MarketingPerformanceChart(records: records),
            const SizedBox(height: 14),
            const Text('Recorded center indicators', style: TextStyle(color: marketingInk, fontSize: 15, fontWeight: FontWeight.w900)),
            const SizedBox(height: 9),
            ...records.map((record) {
              final enrolled = marketingNumber(record['students_enrolled']);
              final target = marketingNumber(record['enrollment_target']);
              final percent = target == 0 ? 0 : (100 * enrolled / target).round();
              final progress = target == 0 ? 0.0 : (enrolled / target).clamp(0.0, 1.0).toDouble();
              return Padding(
                padding: const EdgeInsets.only(bottom: 9),
                child: Container(
                  padding: const EdgeInsets.all(13),
                  decoration: BoxDecoration(color: Colors.white, border: Border.all(color: marketingLine)),
                  child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
                    Wrap(alignment: WrapAlignment.spaceBetween, crossAxisAlignment: WrapCrossAlignment.center, spacing: 10, runSpacing: 8, children: [
                      SizedBox(width: 260, child: Row(children: [
                        Container(width: 38, height: 38, color: const Color(0xFFEAF3EE), child: const Icon(Icons.storefront_outlined, color: marketingGreen)),
                        const SizedBox(width: 10),
                        Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                          Text('${record['center_name'] ?? 'Service center'}', maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: marketingInk, fontWeight: FontWeight.w800)),
                          Text('${record['campus_name'] ?? 'Campus'} • ${_month(record['report_month'])} ${record['report_year']}', maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: marketingMuted, fontSize: 11)),
                        ])),
                      ])),
                      Container(color: const Color(0xFFEAF3EE), padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
                        child: Text(target == 0 ? '$enrolled enrolled' : '$percent% of target', style: const TextStyle(color: marketingGreen, fontWeight: FontWeight.w800, fontSize: 12))),
                    ]),
                    const SizedBox(height: 11),
                    ClipRect(child: LinearProgressIndicator(value: progress, minHeight: 5, backgroundColor: marketingLine, color: marketingGreen)),
                    const SizedBox(height: 9),
                    Wrap(spacing: 16, runSpacing: 6, children: [
                      _Indicator(icon: Icons.forum_outlined, label: 'Inquiries', value: marketingNumber(record['student_inquiries'])),
                      _Indicator(icon: Icons.assignment_outlined, label: 'Applications', value: marketingNumber(record['applications_started'])),
                      _Indicator(icon: Icons.school_outlined, label: 'Enrolled', value: enrolled),
                      _Indicator(icon: Icons.flag_outlined, label: 'Target', value: target),
                    ]),
                  ]),
                ),
              );
            }),
          ],
        ],
      );
}

class _Indicator extends StatelessWidget {
  const _Indicator({required this.icon, required this.label, required this.value});
  final IconData icon;
  final String label;
  final int value;
  @override
  Widget build(BuildContext context) => Row(mainAxisSize: MainAxisSize.min, children: [
    Icon(icon, color: marketingMuted, size: 15),
    const SizedBox(width: 5),
    Text('$label ', style: const TextStyle(color: marketingMuted, fontSize: 11)),
    Text('$value', style: const TextStyle(color: marketingInk, fontSize: 11, fontWeight: FontWeight.w800)),
  ]);
}
