import 'package:flutter/material.dart';
import '../widgets/marketing_ui.dart';
import '../widgets/marketing_trend_chart.dart';
import 'marketing_page_common.dart';

class MarketingAnalyticsPage extends StatelessWidget {
  const MarketingAnalyticsPage({super.key,required this.trend,required this.rankings,required this.summary,required this.years,required this.year,required this.month,required this.onYear,required this.onMonth,required this.onApply});
  final List<Map<String,dynamic>> trend,rankings; final Map<String,dynamic> summary; final List<dynamic> years; final String year,month; final ValueChanged<String?> onYear,onMonth; final VoidCallback onApply;
  InputDecoration _decoration(String label)=>InputDecoration(labelText:label,isDense:true,filled:true,fillColor:Colors.white,border:const OutlineInputBorder(borderRadius:BorderRadius.zero),enabledBorder:const OutlineInputBorder(borderRadius:BorderRadius.zero,borderSide:BorderSide(color:marketingLine)),focusedBorder:const OutlineInputBorder(borderRadius:BorderRadius.zero,borderSide:BorderSide(color:marketingGreen,width:1.5)));
  @override Widget build(BuildContext context)=>MarketingPageFrame(title:'Analytics',subtitle:'Monthly enrollment trends and service center comparisons',icon:Icons.query_stats_outlined,children:[
    Wrap(spacing:10,runSpacing:10,crossAxisAlignment:WrapCrossAlignment.center,children:[
      SizedBox(width:170,child:DropdownButtonFormField<String>(value:year,isExpanded:true,decoration:_decoration('Year'),items:years.map((v)=>DropdownMenuItem(value:'$v',child:Text('$v'))).toList(),onChanged:onYear)),
      SizedBox(width:190,child:DropdownButtonFormField<String>(value:month,isExpanded:true,decoration:_decoration('Month'),items:List.generate(12,(i)=>DropdownMenuItem(value:'${i+1}',child:Text(['January','February','March','April','May','June','July','August','September','October','November','December'][i]))),onChanged:onMonth)),
      SizedBox(height:48,child:ElevatedButton.icon(onPressed:onApply,icon:const Icon(Icons.filter_alt_outlined,size:18),label:const Text('View month'),style:ElevatedButton.styleFrom(backgroundColor:marketingGreen,foregroundColor:Colors.white,elevation:0,shape:const RoundedRectangleBorder(borderRadius:BorderRadius.zero)))),
    ]),
    const SizedBox(height:12),SizedBox(height:300,child:MarketingTrendChart(rows:trend)),const SizedBox(height:12),
    MarketingMetricCard(title:'Selected month enrollment target attainment',value:'${summary['target_attainment']??'—'}${summary['target_attainment']==null?'':'%'}',icon:Icons.flag_outlined),const SizedBox(height:14),
    const Text('Highest performing service centers this month',style:TextStyle(color:marketingInk,fontSize:15,fontWeight:FontWeight.w900)),const SizedBox(height:8),
    if(rankings.isEmpty)const MarketingEmptyState(text:'No performance figures have been recorded for this month.')else ...rankings.asMap().entries.map((entry){final i=entry.key,r=entry.value;final goal=marketingNumber(r['enrollment_target']);final enrolled=marketingNumber(r['students_enrolled']);final percent=goal==0?null:(100*enrolled/goal).round();return Padding(padding:const EdgeInsets.only(bottom:8),child:marketingRecord(icon:i==0?Icons.emoji_events_outlined:Icons.trending_up_outlined,title:'${i+1}. ${r['center_name']}',detail:'${r['campus_name']} • $enrolled enrolled • ${r['student_inquiries']} inquiries',tag:percent==null?'$enrolled enrolled':'$percent% target'));})
  ]);
}
