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 ReportProfitLossProductProfitability extends StatelessWidget {
  final List<ProfitLossAnalyticProduct> data;
  const ReportProfitLossProductProfitability({super.key, required this.data});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Profitabilitas Produk',
            style: AppStyle.xl.copyWith(
              fontWeight: FontWeight.bold,
              color: AppColor.textPrimary,
            ),
          ),
          const SpaceHeight(4),
          Text(
            'Analisis margin keuntungan per produk',
            style: AppStyle.sm.copyWith(color: AppColor.textSecondary),
          ),
          const SpaceHeight(12),
          Column(
            children: data.take(4).map((product) {
              return _buildProductItem(product);
            }).toList(),
          ),
        ],
      ),
    );
  }

  Widget _buildProductItem(ProfitLossAnalyticProduct product) {
    final profitMargin = safeDouble(product.grossProfitMargin);
    final profitColor = profitMargin >= 35
        ? AppColor.success
        : profitMargin >= 25
        ? AppColor.warning
        : AppColor.error;

    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF8FAFC),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Colors.grey[100]!),
      ),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      product.productName,
                      style: AppStyle.md.copyWith(
                        fontWeight: FontWeight.w600,
                        color: const Color(0xFF0F172A),
                      ),
                    ),
                    const SpaceHeight(2),
                    Text(
                      '${product.quantitySold} unit • ${product.categoryName}',
                      style: AppStyle.sm.copyWith(
                        color: AppColor.textSecondary,
                      ),
                    ),
                  ],
                ),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 8,
                      vertical: 4,
                    ),
                    decoration: BoxDecoration(
                      color: profitColor.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(6),
                    ),
                    child: Text(
                      '${safeRound(profitMargin)}%',
                      style: AppStyle.sm.copyWith(
                        fontWeight: FontWeight.bold,
                        color: profitColor,
                      ),
                    ),
                  ),
                  const SpaceHeight(4),
                  Text(
                    product.grossProfit.toString().currencyFormatRpV2,
                    style: AppStyle.md.copyWith(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                      color: AppColor.primary,
                    ),
                  ),
                ],
              ),
            ],
          ),
          const SpaceHeight(12),
          Row(
            children: [
              Expanded(
                child: _buildProductMetric(
                  'Pendapatan',
                  product.revenue,
                  AppColor.info,
                ),
              ),
              const SpaceWidth(8),
              Expanded(
                child: _buildProductMetric(
                  'Biaya',
                  product.cost,
                  AppColor.error,
                ),
              ),
              const SpaceWidth(8),
              Expanded(
                child: _buildProductMetric(
                  'Laba/Unit',
                  product.profitPerUnit,
                  AppColor.success,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildProductMetric(String label, num value, Color color) {
    return Container(
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: color.withOpacity(0.08),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        children: [
          Text(
            label,
            style: TextStyle(
              fontSize: 10,
              fontWeight: FontWeight.w500,
              color: Colors.grey[600],
            ),
          ),
          const SpaceHeight(2),
          Text(
            value.toString().currencyFormatRpV2,
            style: TextStyle(
              fontSize: 11,
              fontWeight: FontWeight.bold,
              color: color,
            ),
          ),
        ],
      ),
    );
  }
}
