import 'package:flutter/material.dart';
import '../widgets/business_trend_chart.dart';
import '../widgets/business_ui.dart';

class BusinessOverviewPage extends StatelessWidget {
  const BusinessOverviewPage({
    super.key,
    required this.summary,
    required this.trend,
    required this.schools,
    required this.programs,
    required this.onOpenSchools,
    required this.onOpenPrograms,
    required this.onOpenEnrollments,
    required this.onOpenAnalytics,
  });
  final Map<String, dynamic> summary;
  final List<Map<String, dynamic>> trend;
  final List<Map<String, dynamic>> schools;
  final List<Map<String, dynamic>> programs;
  final VoidCallback onOpenSchools;
  final VoidCallback onOpenPrograms;
  final VoidCallback onOpenEnrollments;
  final VoidCallback onOpenAnalytics;

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.sizeOf(context).width;
    final compact = width < 800;
    final cards = [
      BusinessMetricCard(title: 'Students enrolled', value: '${businessNumber(summary['total_students'])}', icon: Icons.groups_outlined),
      BusinessMetricCard(title: 'Male students', value: '${businessNumber(summary['male_students'])}', icon: Icons.person_outline, color: businessBlue),
      BusinessMetricCard(title: 'Female students', value: '${businessNumber(summary['female_students'])}', icon: Icons.person_outline, color: const Color(0xFF8A5BB5)),
      BusinessMetricCard(title: 'Schools / programs', value: '${schools.length} / ${programs.length}', icon: Icons.account_tree_outlined),
    ];

    final metrics = Wrap(spacing: 9, runSpacing: 9, children: cards.map((card) => SizedBox(width: compact ? width - 48 : (width - 75) / 4, child: card)).toList());
    final actions = Wrap(spacing: 8, runSpacing: 8, children: [
      _quickAction('Schools', '${schools.length} recorded', Icons.account_balance_outlined, onOpenSchools),
      _quickAction('Programs', '${programs.length} recorded', Icons.business_center_outlined, onOpenPrograms),
      _quickAction('Enrollments', 'View intake records', Icons.groups_outlined, onOpenEnrollments),
      _quickAction('Analytics', 'View trends', Icons.query_stats_outlined, onOpenAnalytics),
    ]);
    final title = Row(children: [
      const Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text('Business Department Overview', style: TextStyle(color: businessGreen, fontSize: 20, fontWeight: FontWeight.w900)),
        Text('Business schools, programs and intake enrollment at a glance', style: TextStyle(color: businessMuted, fontSize: 12)),
      ])),
      IconButton(tooltip: 'Open analytics', onPressed: onOpenAnalytics, icon: const Icon(Icons.query_stats_outlined, color: businessGreen)),
    ]);

    if (compact) {
      return ListView(padding: EdgeInsets.zero, children: [
        title,
        const SizedBox(height: 12),
        metrics,
        const SizedBox(height: 12),
        actions,
        const SizedBox(height: 12),
        SizedBox(height: 270, child: BusinessTrendChart(rows: trend)),
        const SizedBox(height: 12),
        _summaryPanel(summary),
      ]);
    }

    return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
      title,
      const SizedBox(height: 12),
      metrics,
      const SizedBox(height: 12),
      actions,
      const SizedBox(height: 12),
      Expanded(child: Row(children: [
        Expanded(flex: 7, child: BusinessTrendChart(rows: trend)),
        const SizedBox(width: 12),
        Expanded(flex: 4, child: _summaryPanel(summary)),
      ])),
    ]);
  }

  Widget _quickAction(String title, String detail, IconData icon, VoidCallback onTap) => Material(
    color: Colors.white,
    child: InkWell(onTap: onTap, child: Container(
      constraints: const BoxConstraints(minWidth: 155),
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
      decoration: BoxDecoration(border: Border.all(color: businessLine)),
      child: Row(mainAxisSize: MainAxisSize.min, children: [
        Icon(icon, color: businessGreen, size: 20), const SizedBox(width: 9),
        Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Text(title, style: const TextStyle(color: businessInk, fontWeight: FontWeight.w800, fontSize: 12)),
          Text(detail, style: const TextStyle(color: businessMuted, fontSize: 10)),
        ]),
      ]),
    )),
  );

  Widget _summaryPanel(Map<String, dynamic> values) => Container(
    padding: const EdgeInsets.all(17),
    decoration: BoxDecoration(color: Colors.white, border: Border.all(color: businessLine)),
    child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      const Text('Year comparison', style: TextStyle(color: businessInk, fontWeight: FontWeight.w800, fontSize: 15)),
      const SizedBox(height: 8),
      const Text('Current academic intake data compared with the previous year.', style: TextStyle(color: businessMuted, fontSize: 12)),
      const SizedBox(height: 18),
      _line('Current year', '${values['current_year'] ?? '—'}'),
      _line('Previous year', '${values['previous_year'] ?? '—'}'),
      _line('Year-on-year change', values['growth_direction'] == 'new' ? 'New data' : '${values['growth_percent'] ?? 0}%'),
    ]),
  );

  Widget _line(String label, String value) => Padding(
    padding: const EdgeInsets.only(bottom: 13),
    child: Row(children: [Expanded(child: Text(label, style: const TextStyle(color: businessMuted, fontSize: 12))), Text(value, style: const TextStyle(color: businessGreen, fontWeight: FontWeight.w800))]),
  );
}
