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

import '../../../../common/theme/theme.dart';

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

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(16),
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: AppColor.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: AppColor.textLight.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: AppColor.info.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Icon(
                  LineIcons.calculator,
                  color: AppColor.info,
                  size: 20,
                ),
              ),
              const SizedBox(width: 12),
              Text(
                'Detail Profit & Loss',
                style: AppStyle.lg.copyWith(fontWeight: FontWeight.bold),
              ),
            ],
          ),
          const SizedBox(height: 20),

          _buildPLItem(
            'Penjualan Kotor',
            'Rp 25.840.000',
            AppColor.success,
            true,
          ),
          _buildPLItem('Diskon & Retur', '- Rp 560.000', AppColor.error, false),
          const Divider(height: 24),
          _buildPLItem(
            'Penjualan Bersih',
            'Rp 25.280.000',
            AppColor.textPrimary,
            true,
            isHeader: true,
          ),
          const SizedBox(height: 12),
          _buildPLItem(
            'HPP (Harga Pokok Penjualan)',
            '- Rp 15.120.000',
            AppColor.error,
            false,
          ),
          const Divider(height: 24),
          _buildPLItem(
            'Laba Kotor',
            'Rp 10.160.000',
            AppColor.success,
            true,
            isHeader: true,
          ),
          const SizedBox(height: 12),
          _buildPLItem(
            'Biaya Operasional',
            '- Rp 2.640.000',
            AppColor.error,
            false,
          ),
          const Divider(height: 24),
          _buildPLItem(
            'Laba Bersih',
            'Rp 7.520.000',
            AppColor.primary,
            true,
            isHeader: true,
            showPercentage: true,
            percentage: '29.8%',
          ),
        ],
      ),
    );
  }

  Widget _buildPLItem(
    String title,
    String amount,
    Color color,
    bool isPositive, {
    bool isHeader = false,
    bool showPercentage = false,
    String? percentage,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Text(
              title,
              style: isHeader
                  ? AppStyle.md.copyWith(
                      fontWeight: FontWeight.bold,
                      color: color,
                    )
                  : AppStyle.md.copyWith(color: AppColor.textSecondary),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                amount,
                style: isHeader
                    ? AppStyle.lg.copyWith(
                        fontWeight: FontWeight.bold,
                        color: color,
                      )
                    : AppStyle.md.copyWith(
                        color: color,
                        fontWeight: FontWeight.w600,
                      ),
              ),
              if (showPercentage && percentage != null)
                Text(
                  percentage,
                  style: AppStyle.xs.copyWith(
                    color: AppColor.textSecondary,
                    fontStyle: FontStyle.italic,
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }
}
