import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:barber_app/core/theme/app_theme.dart';
import 'package:barber_app/features/finances/data/repositories/transaction_repository.dart';
import 'package:intl/intl.dart';

class RevenueChartNew extends StatefulWidget {
  const RevenueChartNew({super.key});

  @override
  State<RevenueChartNew> createState() => _RevenueChartNewState();
}

class _RevenueChartNewState extends State<RevenueChartNew> {
  final TransactionRepository _repository = TransactionRepository();

  @override
  Widget build(BuildContext context) {
    final last7Days = _repository.getLast7DaysRevenue();
    final now = DateTime.now();

    final List<DailyRevenue> data = List.generate(7, (index) {
      final date = now.subtract(Duration(days: 6 - index));
      return DailyRevenue(
        day: DateFormat(
          'E',
          'pt_BR',
        ).format(date).substring(0, 3).toUpperCase(),
        amount: last7Days[index],
      );
    });

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: BorderRadius.circular(24),
        border: Border.all(color: AppColors.surfaceLight),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.2),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: SfCartesianChart(
        plotAreaBorderWidth: 0,
        primaryXAxis: CategoryAxis(
          majorGridLines: const MajorGridLines(width: 0),
          axisLine: const AxisLine(width: 0),
          labelStyle: const TextStyle(
            color: AppColors.textSecondary,
            fontSize: 10,
          ),
        ),
        primaryYAxis: NumericAxis(
          isVisible: true,
          majorGridLines: MajorGridLines(
            width: 1,
            color: AppColors.surfaceLight.withValues(alpha: 0.5),
          ),
          axisLine: const AxisLine(width: 0),
          labelStyle: const TextStyle(
            color: AppColors.textSecondary,
            fontSize: 10,
          ),
          numberFormat: NumberFormat.compactSimpleCurrency(locale: 'pt_BR'),
        ),
        tooltipBehavior: TooltipBehavior(
          enable: true,
          header: '',
          format: 'point.y',
          color: AppColors.surfaceLight,
          textStyle: const TextStyle(color: AppColors.textPrimary),
          builder:
              (
                dynamic data,
                dynamic point,
                dynamic series,
                int pointIndex,
                int seriesIndex,
              ) {
                final DailyRevenue item = data;
                return Container(
                  padding: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    color: AppColors.surfaceLight,
                    borderRadius: BorderRadius.circular(8),
                    boxShadow: const [
                      BoxShadow(color: Colors.black26, blurRadius: 4),
                    ],
                  ),
                  child: Text(
                    '${item.day}: ${NumberFormat.currency(symbol: 'R\$', locale: 'pt_BR').format(item.amount)}',
                    style: const TextStyle(
                      color: AppColors.textPrimary,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                );
              },
        ),
        series: <CartesianSeries>[
          SplineAreaSeries<DailyRevenue, String>(
            dataSource: data,
            xValueMapper: (DailyRevenue data, _) => data.day,
            yValueMapper: (DailyRevenue data, _) => data.amount,
            gradient: LinearGradient(
              colors: [
                AppColors.primaryColor.withValues(alpha: 0.5),
                AppColors.primaryColor.withValues(alpha: 0.0),
              ],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
            borderColor: AppColors.primaryColor,
            borderWidth: 3,
            name: 'Receita',
            animationDuration: 1000,
            markerSettings: MarkerSettings(
              isVisible: true,
              height: 8,
              width: 8,
              color: AppColors.surface,
              borderColor: AppColors.primaryColor,
              borderWidth: 2,
            ),
          ),
        ],
      ),
    );
  }
}

class DailyRevenue {
  final String day;
  final double amount;

  DailyRevenue({required this.day, required this.amount});
}
