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

class BusinessAnalyticsPage extends StatelessWidget {
  const BusinessAnalyticsPage({
    super.key,
    required this.summary,
    required this.trend,
    required this.schools,
    required this.programs,
    required this.years,
    required this.schoolId,
    required this.programId,
    required this.year,
    required this.onSchool,
    required this.onProgram,
    required this.onYear,
    required this.onApply,
  });
  final Map<String, dynamic> summary;
  final List<Map<String, dynamic>> trend;
  final List<Map<String, dynamic>> schools;
  final List<Map<String, dynamic>> programs;
  final List<dynamic> years;
  final String schoolId;
  final String programId;
  final String year;
  final ValueChanged<String?> onSchool;
  final ValueChanged<String?> onProgram;
  final ValueChanged<String?> onYear;
  final VoidCallback onApply;

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.sizeOf(context).width;
    final total = businessNumber(summary['total_students']);
    final male = businessNumber(summary['male_students']);
    final female = businessNumber(summary['female_students']);
    final growth = double.tryParse('${summary['growth_percent']}') ?? 0;
    final direction = summary['growth_direction']?.toString() ?? 'stable';
    final label = direction == 'new' ? 'New records' : '${growth >= 0 ? '+' : ''}${growth.toStringAsFixed(1)}%';

    final heading = const BusinessPageHeading(title: 'Enrollment Analytics', subtitle: 'Business school intake patterns and year-on-year change', icon: Icons.query_stats_outlined);
    final filter = BusinessFilterBar(schools: schools, programs: programs, years: years, schoolId: schoolId, programId: programId, year: year,
      onSchool: onSchool, onProgram: onProgram, onYear: onYear, onApply: onApply);
    final cards = Wrap(spacing: 9, runSpacing: 9, children: [
      SizedBox(width: width < 800 ? width - 48 : 205, child: BusinessMetricCard(title: 'Total students', value: '$total', icon: Icons.groups_outlined)),
      SizedBox(width: width < 800 ? width - 48 : 205, child: BusinessMetricCard(title: 'Male students', value: '$male', icon: Icons.person_outline, color: businessBlue)),
      SizedBox(width: width < 800 ? width - 48 : 205, child: BusinessMetricCard(title: 'Female students', value: '$female', icon: Icons.person_outline, color: const Color(0xFF8A5BB5))),
      SizedBox(width: width < 800 ? width - 48 : 205, child: BusinessMetricCard(title: 'Change vs prior year', value: label, icon: direction == 'decreasing' ? Icons.trending_down : Icons.trending_up,
        color: direction == 'decreasing' ? const Color(0xFFB3261E) : businessGreen)),
    ]);
    if (width < 800) {
      return ListView(padding: EdgeInsets.zero, children: [
        heading, const SizedBox(height: 13), filter, const SizedBox(height: 12), cards,
        const SizedBox(height: 12), SizedBox(height: 330, child: BusinessTrendChart(rows: trend)),
      ]);
    }
    return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
      heading, const SizedBox(height: 13), filter, const SizedBox(height: 12), cards,
      const SizedBox(height: 12), Expanded(child: BusinessTrendChart(rows: trend)),
    ]);
  }
}
