import 'package:siro_driver/constant/currency.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:fl_chart/fl_chart.dart';
import '../../../../constant/finance_design_system.dart';
import '../../../../controller/home/statistics/statistics_controller.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<StatisticsController>(
      builder: (sc) {
        return Container(
          padding: const EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: FinanceDesignSystem.cardColor,
            borderRadius: BorderRadius.circular(FinanceDesignSystem.cardRadius),
            boxShadow: [
              BoxShadow(
                  color: Colors.black.withOpacity(0.03),
                  blurRadius: 10,
                  offset: const Offset(0, 4))
            ],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
                Text('Weekly Earnings'.tr,
                    style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: FinanceDesignSystem.primaryDark)),
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                  decoration: BoxDecoration(
                      color: FinanceDesignSystem.successGreen.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(12)),
                  child: Text(
                      '${sc.weeklyEarnings.toStringAsFixed(0)} ${CurrencyHelper.currency}',
                      style: TextStyle(
                          fontSize: 12,
                          fontWeight: FontWeight.bold,
                          color: FinanceDesignSystem.successGreen)),
                ),
              ]),
              const SizedBox(height: 8),
              Row(children: [
                _miniStat(Icons.local_taxi_rounded, '${sc.weeklyTrips}',
                    'Rides'.tr, FinanceDesignSystem.accentBlue),
                const SizedBox(width: 16),
                _miniStat(
                    Icons.timer_rounded,
                    '${sc.weeklyHours.toStringAsFixed(1)}h',
                    'Hours'.tr,
                    const Color(0xFFFF9800)),
              ]),
              const SizedBox(height: 20),
              SizedBox(
                height: 180,
                child: sc.weeklyStats.isEmpty
                    ? Center(
                        child: Text('No data yet'.tr,
                            style: TextStyle(color: Colors.grey.shade400)))
                    : BarChart(
                        BarChartData(
                          alignment: BarChartAlignment.spaceAround,
                          maxY: _getMaxY(sc.weeklyStats),
                          barTouchData: BarTouchData(
                            enabled: true,
                            touchTooltipData: BarTouchTooltipData(
                              getTooltipItem: (group, gi, rod, ri) =>
                                  BarTooltipItem(
                                '${rod.toY.toStringAsFixed(0)} ${CurrencyHelper.currency}',
                                const TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 12),
                              ),
                            ),
                          ),
                          titlesData: FlTitlesData(
                            show: true,
                            bottomTitles: AxisTitles(
                                sideTitles: SideTitles(
                                    showTitles: true,
                                    getTitlesWidget: (v, m) {
                                      final idx = v.toInt();
                                      if (idx >= 0 &&
                                          idx < sc.weeklyStats.length) {
                                        return Padding(
                                            padding:
                                                const EdgeInsets.only(top: 8),
                                            child: Text(
                                                sc.weeklyStats[idx].dayName.tr,
                                                style: TextStyle(
                                                    fontSize: 10,
                                                    color: FinanceDesignSystem
                                                        .textSecondary)));
                                      }
                                      return const SizedBox.shrink();
                                    })),
                            leftTitles: const AxisTitles(
                                sideTitles: SideTitles(showTitles: false)),
                            topTitles: const AxisTitles(
                                sideTitles: SideTitles(showTitles: false)),
                            rightTitles: const AxisTitles(
                                sideTitles: SideTitles(showTitles: false)),
                          ),
                          borderData: FlBorderData(show: false),
                          gridData: const FlGridData(show: false),
                          barGroups: List.generate(sc.weeklyStats.length, (i) {
                            final stat = sc.weeklyStats[i];
                            final isToday = i == sc.weeklyStats.length - 1;
                            return BarChartGroupData(x: i, barRods: [
                              BarChartRodData(
                                toY: stat.earnings,
                                width: 20,
                                borderRadius: const BorderRadius.vertical(
                                    top: Radius.circular(6)),
                                gradient: LinearGradient(
                                  begin: Alignment.bottomCenter,
                                  end: Alignment.topCenter,
                                  colors: isToday
                                      ? [
                                          FinanceDesignSystem.accentBlue,
                                          const Color(0xFF82B1FF)
                                        ]
                                      : [
                                          FinanceDesignSystem.primaryDark
                                              .withOpacity(0.6),
                                          FinanceDesignSystem.primaryDark
                                              .withOpacity(0.3)
                                        ],
                                ),
                              ),
                            ]);
                          }),
                        ),
                      ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _miniStat(IconData icon, String value, String label, Color color) {
    return Row(children: [
      Icon(icon, size: 16, color: color),
      const SizedBox(width: 4),
      Text(value,
          style: TextStyle(
              fontSize: 13,
              fontWeight: FontWeight.bold,
              color: FinanceDesignSystem.primaryDark)),
      const SizedBox(width: 4),
      Text(label,
          style: TextStyle(
              fontSize: 11, color: FinanceDesignSystem.textSecondary)),
    ]);
  }

  double _getMaxY(List<DayStat> stats) {
    if (stats.isEmpty) return 100;
    final max = stats.map((s) => s.earnings).reduce((a, b) => a > b ? a : b);
    return max <= 0 ? 100 : max * 1.2;
  }
}
