import 'package:flutter/material.dart';

import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../domain/analytic/analytic.dart';

class ProfitLossProduct extends StatelessWidget {
  final ProfitLossProductData product;
  const ProfitLossProduct({super.key, required this.product});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColor.background,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColor.border.withOpacity(0.5)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Product header
          Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      product.productName,
                      style: AppStyle.md.copyWith(fontWeight: FontWeight.bold),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 2),
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 8,
                        vertical: 2,
                      ),
                      decoration: BoxDecoration(
                        color: AppColor.primary.withOpacity(0.1),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Text(
                        product.categoryName,
                        style: AppStyle.xs.copyWith(color: AppColor.primary),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(width: 12),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    '${product.quantitySold} terjual',
                    style: AppStyle.sm.copyWith(color: AppColor.textSecondary),
                  ),
                  Text(
                    '${product.grossProfitMargin.toStringAsFixed(1)}%',
                    style: AppStyle.md.copyWith(
                      fontWeight: FontWeight.bold,
                      color: product.grossProfitMargin > 25
                          ? AppColor.success
                          : product.grossProfitMargin > 15
                          ? AppColor.warning
                          : AppColor.error,
                    ),
                  ),
                ],
              ),
            ],
          ),
          const SizedBox(height: 16),

          // Financial metrics
          Row(
            children: [
              Expanded(
                child: _buildMetricColumn(
                  context.lang.revenue,
                  product.revenue.currencyFormatRp,
                  AppColor.success,
                ),
              ),
              Expanded(
                child: _buildMetricColumn(
                  context.lang.cost,
                  product.cost.currencyFormatRp,
                  AppColor.error,
                ),
              ),
              Expanded(
                child: _buildMetricColumn(
                  context.lang.gross_profit,
                  product.grossProfit.currencyFormatRp,
                  AppColor.info,
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),

          // Average metrics
          Row(
            children: [
              Expanded(
                child: _buildMetricColumn(
                  context.lang.average_price,
                  product.averagePrice.currencyFormatRp,
                  AppColor.textSecondary,
                ),
              ),
              Expanded(
                child: _buildMetricColumn(
                  context.lang.profit_per_unit,
                  product.profitPerUnit.currencyFormatRp,
                  AppColor.primary,
                ),
              ),
              const Expanded(child: SizedBox()),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildMetricColumn(String label, String value, Color color) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(label, style: AppStyle.xs.copyWith(color: AppColor.textSecondary)),
        const SizedBox(height: 2),
        Text(
          value,
          style: AppStyle.sm.copyWith(
            fontWeight: FontWeight.w600,
            color: color,
          ),
        ),
      ],
    );
  }
}
