import 'package:flutter/material.dart';

import '../../../../../../../common/theme/theme.dart';
import '../../../../../../../domain/analytic/analytic.dart';
import '../../../../../../components/spaces/space.dart';
import '../../../../../../components/widgets/report/report_summary_card.dart';

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Expanded(
              child: ReportSummaryCard(
                color: AppColor.success,
                icon: Icons.warning_amber_outlined,
                title: 'Low Stok',
                value: data.summary.lowStockIngredients.toString(),
              ),
            ),
            const SpaceWidth(12),
            Expanded(
              child: ReportSummaryCard(
                color: AppColor.error,
                icon: Icons.block_outlined,
                title: 'Zero Stok',
                value: data.summary.zeroStockIngredients.toString(),
              ),
            ),
            const SpaceWidth(12),
            Expanded(
              child: ReportSummaryCard(
                color: AppColor.error,
                icon: Icons.shopping_cart_checkout_outlined,
                title: 'Total Sold',
                value: data.summary.totalSoldIngredients.toString(),
              ),
            ),
          ],
        ),
        Container(
          margin: const EdgeInsets.only(top: 16),
          decoration: const BoxDecoration(
            color: AppColor.primary, // Purple color
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8),
              topRight: Radius.circular(8),
            ),
          ),
          child: Table(
            columnWidths: const {
              0: FlexColumnWidth(2.5), // Name
              1: FlexColumnWidth(1), // Stock
              2: FlexColumnWidth(2), // Masuk
              3: FlexColumnWidth(2), // Keluar
            },
            children: [
              TableRow(
                children: [
                  _buildHeaderCell('Nama'),
                  _buildHeaderCell('Stock'),
                  _buildHeaderCell('Masuk'),
                  _buildHeaderCell('Keluar'),
                ],
              ),
            ],
          ),
        ),
        Container(
          decoration: const BoxDecoration(color: AppColor.white),
          child: Table(
            columnWidths: const {
              0: FlexColumnWidth(2.5), // Name
              1: FlexColumnWidth(1), // Stock
              2: FlexColumnWidth(2), // Masuk
              3: FlexColumnWidth(2), // Keluar
            },
            children: data.ingredients
                .map(
                  (item) => _buildIngredientsDataRow(
                    item,
                    data.ingredients.indexOf(item) % 2 == 0,
                  ),
                )
                .toList(),
          ),
        ),
        Container(
          decoration: const BoxDecoration(
            color: AppColor.primary, // Purple color
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(8),
              bottomRight: Radius.circular(8),
            ),
          ),
          child: Table(
            columnWidths: const {
              0: FlexColumnWidth(2.5), // Name
              1: FlexColumnWidth(1), // Stock
              2: FlexColumnWidth(2), // Masuk
              3: FlexColumnWidth(2), // Keluar
            },
            children: [
              TableRow(
                children: [
                  _buildTotalCell('TOTAL'),
                  _buildTotalCell(
                    (data.ingredients.fold<num>(
                      0,
                      (sum, item) => sum + (item.quantity),
                    )).toString(),
                  ),
                  _buildTotalCell(
                    (data.ingredients.fold<num>(
                      0,
                      (sum, item) => sum + (item.totalIn),
                    )).toString(),
                  ),
                  _buildTotalCell(
                    (data.ingredients.fold<num>(
                      0,
                      (sum, item) => sum + (item.totalOut),
                    )).toString(),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  TableRow _buildIngredientsDataRow(
    InventoryAnalyticIngredientItem item,
    bool isEven,
  ) {
    return TableRow(
      decoration: BoxDecoration(
        color: item.isZeroStock
            ? Colors.red.shade100
            : item.isLowStock
            ? Colors.yellow.shade100
            : isEven
            ? Colors.grey.shade50
            : AppColor.white,
      ),
      children: [
        _buildDataCell(item.ingredientName, alignment: Alignment.centerLeft),
        _buildDataCell(item.quantity.toString()),
        _buildDataCell(item.totalIn.toString()),
        _buildDataCell(item.totalOut.toString()),
      ],
    );
  }

  Widget _buildDataCell(
    String text, {
    Alignment alignment = Alignment.center,
    Color? textColor,
  }) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
      alignment: alignment,
      child: Text(
        text,
        style: AppStyle.sm.copyWith(
          color: textColor ?? AppColor.black,
          fontWeight: FontWeight.normal,
        ),
        textAlign: alignment == Alignment.centerLeft
            ? TextAlign.left
            : TextAlign.center,
      ),
    );
  }

  Widget _buildHeaderCell(String text) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
      child: Text(
        text,
        style: AppStyle.sm.copyWith(
          color: AppColor.white,
          fontWeight: FontWeight.bold,
        ),
        textAlign: TextAlign.center,
      ),
    );
  }

  Widget _buildTotalCell(String text) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
      child: Text(
        text,
        style: AppStyle.sm.copyWith(
          color: AppColor.white,
          fontWeight: FontWeight.bold,
        ),
        textAlign: TextAlign.center,
      ),
    );
  }
}
