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

import '../../../../../../../common/extension/extension.dart';
import '../../../../../../../common/function/app_function.dart';
import '../../../../../../../common/theme/theme.dart';
import '../../../../../../../domain/analytic/analytic.dart';
import '../../../../../../components/spaces/space.dart';

class ReportProfitLossBreakdown extends StatelessWidget {
  final ProfitLossAnalytic data;
  const ReportProfitLossBreakdown({super.key, required this.data});

  @override
  Widget build(BuildContext context) {
    final breakdownData = [
      {
        'label': 'Laba Kotor',
        'value': data.summary.grossProfit,
        'color': AppColor.success,
      },
      {
        'label': 'Pajak',
        'value': data.summary.totalTax,
        'color': AppColor.warning,
      },
      {
        'label': 'Diskon',
        'value': data.summary.totalDiscount,
        'color': AppColor.info,
      },
      {
        'label': 'Laba Bersih',
        'value': data.summary.netProfit,
        'color': AppColor.primary,
      },
    ];

    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Rincian Keuntungan',
            style: AppStyle.xl.copyWith(
              fontWeight: FontWeight.bold,
              color: AppColor.textPrimary,
            ),
          ),
          const SpaceHeight(4),
          Text(
            'Komponen pembentuk profit',
            style: AppStyle.sm.copyWith(color: AppColor.textSecondary),
          ),
          const SpaceHeight(20),

          // Grafik Donut
          SizedBox(
            height: 160,
            child: PieChart(
              PieChartData(
                sectionsSpace: 3,
                centerSpaceRadius: 50,
                startDegreeOffset: -90,
                sections: breakdownData.asMap().entries.map((entry) {
                  final item = entry.value;
                  final grossProfit = data.summary.grossProfit;
                  final value = item['value'] as num;

                  // Handle division by zero
                  final percentage = grossProfit > 0
                      ? (value / grossProfit * 100)
                      : 0.0;

                  return PieChartSectionData(
                    color: item['color'] as Color,
                    value: value.toDouble(),
                    title: '${safeRound(percentage)}%',
                    radius: 40,
                    titleStyle: AppStyle.xs.copyWith(
                      fontSize: 10,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  );
                }).toList(),
              ),
            ),
          ),

          const SpaceHeight(16),

          // Legenda
          Column(
            children: breakdownData.map((item) {
              return Padding(
                padding: const EdgeInsets.only(bottom: 12),
                child: Row(
                  children: [
                    Container(
                      width: 12,
                      height: 12,
                      decoration: BoxDecoration(
                        color: item['color'] as Color,
                        borderRadius: BorderRadius.circular(2),
                      ),
                    ),
                    const SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        item['label'] as String,
                        style: AppStyle.sm.copyWith(
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                    Text(
                      (item['value'] as num).toString().currencyFormatRpV2,
                      style: AppStyle.sm.copyWith(
                        fontWeight: FontWeight.bold,
                        color: AppColor.textPrimary,
                      ),
                    ),
                  ],
                ),
              );
            }).toList(),
          ),

          const SpaceHeight(16),

          // Rasio Profitabilitas
          Container(
            width: double.infinity,
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  AppColor.primary.withOpacity(0.1),
                  AppColor.secondary.withOpacity(0.1),
                ],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Column(
              children: [
                Text(
                  'Rasio Profitabilitas',
                  style: AppStyle.sm.copyWith(
                    fontSize: 12,
                    fontWeight: FontWeight.w600,
                    color: AppColor.textSecondary,
                  ),
                ),
                const SpaceHeight(8),
                Text(
                  '${safeRound(data.summary.profitabilityRatio)}%',
                  style: AppStyle.h5.copyWith(
                    fontWeight: FontWeight.bold,
                    color: AppColor.primary,
                  ),
                ),
                const SpaceHeight(4),
                Text(
                  'Tingkat Pengembalian Pendapatan',
                  style: AppStyle.xs.copyWith(
                    fontSize: 10,
                    color: AppColor.textSecondary,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
