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 ReportInventoryProductTab extends StatelessWidget {
  final InventoryAnalytic data;
  const ReportInventoryProductTab({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.lowStockProducts.toString(),
              ),
            ),
            const SpaceWidth(12),
            Expanded(
              child: ReportSummaryCard(
                color: AppColor.error,
                icon: Icons.block_outlined,
                title: 'Zero Stok',
                value: data.summary.zeroStockProducts.toString(),
              ),
            ),
            const SpaceWidth(12),
            Expanded(
              child: ReportSummaryCard(
                color: AppColor.error,
                icon: Icons.shopping_cart_checkout_outlined,
                title: 'Total Sold',
                value: data.summary.totalSoldProducts.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), // Produk
              1: FlexColumnWidth(2), // Kategori
              2: FlexColumnWidth(1), // Stock
              3: FlexColumnWidth(2), // Masuk
              4: FlexColumnWidth(2), // Keluar
            },
            children: [
              TableRow(
                children: [
                  _buildHeaderCell('Nama'),
                  _buildHeaderCell('Kategori'),
                  _buildHeaderCell('Stock'),
                  _buildHeaderCell('Masuk'),
                  _buildHeaderCell('Keluar'),
                ],
              ),
            ],
          ),
        ),
        Container(
          decoration: const BoxDecoration(color: AppColor.white),
          child: Table(
            columnWidths: const {
              0: FlexColumnWidth(2.5), // Produk
              1: FlexColumnWidth(2), // Kategori
              2: FlexColumnWidth(1), // Stock
              3: FlexColumnWidth(2), // Masuk
              4: FlexColumnWidth(2), // Keluar
            },
            children: data.products
                .map(
                  (item) => _buildProductDataRow(
                    item,
                    data.products.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), // Produk
              1: FlexColumnWidth(2), // Kategori
              2: FlexColumnWidth(1), // Stock
              3: FlexColumnWidth(2), // Masuk
              4: FlexColumnWidth(2), // Keluar
            },
            children: [
              TableRow(
                children: [
                  _buildTotalCell('TOTAL'),
                  _buildTotalCell(''),
                  _buildTotalCell(
                    (data.products.fold<num>(
                      0,
                      (sum, item) => sum + (item.quantity),
                    )).toString(),
                  ),
                  _buildTotalCell(
                    (data.products.fold<num>(
                      0,
                      (sum, item) => sum + (item.totalIn),
                    )).toString(),
                  ),
                  _buildTotalCell(
                    (data.products.fold<num>(
                      0,
                      (sum, item) => sum + (item.totalOut),
                    )).toString(),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }

  TableRow _buildProductDataRow(
    InventoryAnalyticProductItem product,
    bool isEven,
  ) {
    return TableRow(
      decoration: BoxDecoration(
        color: product.isZeroStock
            ? Colors.red.shade100
            : product.isLowStock
            ? Colors.yellow.shade100
            : isEven
            ? Colors.grey.shade50
            : AppColor.white,
      ),
      children: [
        _buildDataCell(product.productName, alignment: Alignment.centerLeft),
        _buildDataCell(product.categoryName, alignment: Alignment.centerLeft),
        _buildDataCell(product.quantity.toString()),
        _buildDataCell(product.totalIn.toString()),
        _buildDataCell(product.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,
      ),
    );
  }
}
