// lib/features/admin/tabs/kpi/executive_overview_panel.dart

import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import 'sharp_kpi_card.dart';
import 'kpi_trend_chart.dart';

const Color _standardGold = Color.fromARGB(255, 204, 117, 4);
const Color _darkGreen = Color.fromARGB(255, 2, 105, 48);

class ExecutiveOverviewPanel extends StatelessWidget {
  const ExecutiveOverviewPanel({super.key});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        // --- RESPONSIVE WIDTH CALCULATION ---
        // Subtracting 48 for the 24px padding on both left and right
        final double availableWidth = constraints.maxWidth - 48.0;
        const double spacing = 16.0;

        int crossAxisCount = 1;
        if (availableWidth >= 1200) {
          crossAxisCount = 4;
        } else if (availableWidth >= 800) {
          crossAxisCount = 3;
        } else if (availableWidth >= 600) {
          crossAxisCount = 2;
        }

        // Calculate exact width for each card, floored to prevent sub-pixel rounding overflows
        final double cardWidth = ((availableWidth - (spacing * (crossAxisCount - 1))) / crossAxisCount).floorToDouble();

        return SingleChildScrollView(
          padding: const EdgeInsets.all(24.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // --- PREMIUM SECTION HEADER ---
              Row(
                children: [
                  Container(
                    width: 4,
                    height: 24,
                    color: _standardGold,
                  ),
                  const SizedBox(width: 12),
                  Text(
                    'SYSTEM WIDE METRICS',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.w900,
                      color: Colors.grey.shade800,
                      letterSpacing: 1.5,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 24),

              // --- RESPONSIVE KPI CARDS (Using Wrap instead of GridView to prevent overflow) ---
              SizedBox(
                width: double.infinity,
                child: Wrap(
                  spacing: spacing,
                  runSpacing: spacing,
                  children: [
                    SizedBox(
                      width: cardWidth,
                      child: const SharpKpiCard(
                        title: 'University Health Index',
                        value: '45.0%',
                        targetValue: '100%',
                        subtitle: 'Critical Attention Required',
                        icon: Icons.health_and_safety_rounded,
                        iconColor: Colors.redAccent,
                        isFailing: true,
                      ),
                    ),
                    SizedBox(
                      width: cardWidth,
                      child: const SharpKpiCard(
                        title: 'Active Departments',
                        value: '20',
                        subtitle: 'All departments reporting',
                        icon: Icons.account_balance_rounded,
                        iconColor: _darkGreen,
                      ),
                    ),
                    SizedBox(
                      width: cardWidth,
                      child: const SharpKpiCard(
                        title: 'Total System KPIs',
                        value: '60',
                        subtitle: '3 assigned per department',
                        icon: Icons.insights_rounded,
                        iconColor: _standardGold,
                      ),
                    ),
                    SizedBox(
                      width: cardWidth,
                      child: const SharpKpiCard(
                        title: 'Data Sync Status',
                        value: 'Q1',
                        subtitle: 'Last updated: 4 hrs ago',
                        icon: Icons.sync_rounded,
                        iconColor: _darkGreen,
                      ),
                    ),
                  ],
                ),
              ),
              
              const SizedBox(height: 40),

              // --- WIDE CHART SECTION (Enhanced with shadows and better typography) ---
              Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.zero, // Maintaining sharp edges
                  border: Border.all(color: Colors.grey.shade200, width: 1),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.04),
                      blurRadius: 20,
                      offset: const Offset(0, 8),
                    ),
                  ],
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Premium Chart Header
                    Container(
                      width: double.infinity,
                      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
                      decoration: const BoxDecoration(
                        color: _darkGreen,
                        border: Border(bottom: BorderSide(color: _standardGold, width: 4)),
                      ),
                      child: Row(
                        children: [
                          const Icon(Icons.auto_graph_rounded, color: Colors.white, size: 24),
                          const SizedBox(width: 12),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const Text(
                                'AGGREGATE PERFORMANCE TREND (YTD)',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 15,
                                  fontWeight: FontWeight.bold,
                                  letterSpacing: 1.2,
                                ),
                              ),
                              const SizedBox(height: 4),
                              Text(
                                'Actual performance vs. Institutional targets across all sectors',
                                style: TextStyle(
                                  color: Colors.white.withOpacity(0.8),
                                  fontSize: 12,
                                  fontWeight: FontWeight.w400,
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                    
                    // Chart Body
                    Padding(
                      padding: const EdgeInsets.all(32.0),
                      child: SizedBox(
                        height: 420, // Slightly taller for better data visualization
                        child: KpiTrendChart(
                          maxY: 100,
                          mainLineColor: _darkGreen,
                          secondaryLineColor: _standardGold,
                          mainLineData: const [
                            FlSpot(0, 42), FlSpot(1, 44), FlSpot(2, 45), FlSpot(3, 45),
                          ], // Actuals Q1
                          secondaryLineData: const [
                            FlSpot(0, 50), FlSpot(1, 55), FlSpot(2, 60), FlSpot(3, 65),
                            FlSpot(4, 70), FlSpot(5, 75), FlSpot(6, 80), FlSpot(7, 85),
                            FlSpot(8, 90), FlSpot(9, 95), FlSpot(10, 100), FlSpot(11, 100),
                          ], // Target trajectory
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}