import 'package:flutter/material.dart';

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

class ProductDetailBottomSheet {
  static void show({
    required BuildContext context,
    required ProductAnalyticData product,
    required int totalRevenue,
  }) {
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      barrierColor: Colors.transparent,
      builder: (context) =>
          _ProductDetailContent(product: product, totalRevenue: totalRevenue),
    );
  }
}

class _ProductDetailContent extends StatelessWidget {
  final ProductAnalyticData product;
  final int totalRevenue;

  const _ProductDetailContent({
    required this.product,
    required this.totalRevenue,
  });

  @override
  Widget build(BuildContext context) {
    final contribution = totalRevenue > 0
        ? (product.revenue / totalRevenue * 100)
        : 0.0;

    final hppPercentage = product.revenue > 0
        ? (product.standardHppTotal / product.revenue * 100)
        : 0.0;

    final marginStatus = _getMarginStatus(hppPercentage);

    return Container(
      constraints: BoxConstraints(
        maxHeight: MediaQuery.of(context).size.height * 0.6,
      ),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      child: SingleChildScrollView(
        padding: const EdgeInsets.all(24),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            // Drag handle
            Center(
              child: Container(
                width: 40,
                height: 4,
                margin: const EdgeInsets.only(bottom: 20),
                decoration: BoxDecoration(
                  color: Colors.grey.shade300,
                  borderRadius: BorderRadius.circular(2),
                ),
              ),
            ),

            // Product name
            Text(
              product.productName.toUpperCase(),
              style: AppStyle.xl.copyWith(
                fontWeight: FontWeight.bold,
                color: AppColor.textPrimary,
                fontSize: 20,
              ),
            ),
            const SpaceHeight(4),

            // Category
            Text(
              product.categoryName,
              style: AppStyle.md.copyWith(
                color: AppColor.primary,
                fontWeight: FontWeight.w600,
                fontSize: 14,
              ),
            ),

            const SpaceHeight(20),

            // Details
            _buildRow(context, 'Kategori', product.categoryName),
            _buildDivider(),
            _buildRow(
              context,
              'Qty ${context.lang.sold.toLowerCase()}',
              '${product.quantitySold} porsi',
            ),
            _buildDivider(),
            _buildRow(
              context,
              context.lang.revenue,
              product.revenue.currencyFormatRp,
            ),
            _buildDivider(),
            _buildRow(
              context,
              'Kontribusi omzet',
              '${contribution.toStringAsFixed(1)}%',
            ),
            _buildDivider(),
            _buildRow(
              context,
              '% ${context.lang.hpp} (real)',
              '${hppPercentage.toStringAsFixed(1)}%',
            ),
            _buildDivider(),
            _buildRowWithBadge(
              context,
              'Status margin',
              marginStatus.label,
              marginStatus.color,
            ),

            const SpaceHeight(16),
          ],
        ),
      ),
    );
  }

  Widget _buildRow(BuildContext context, String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 14),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: AppStyle.md.copyWith(
              color: AppColor.textSecondary,
              fontWeight: FontWeight.w500,
              fontSize: 14,
            ),
          ),
          Text(
            value,
            style: AppStyle.md.copyWith(
              color: AppColor.textPrimary,
              fontWeight: FontWeight.w700,
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildRowWithBadge(
    BuildContext context,
    String label,
    String badgeText,
    Color badgeColor,
  ) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 14),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: AppStyle.md.copyWith(
              color: AppColor.textSecondary,
              fontWeight: FontWeight.w500,
              fontSize: 14,
            ),
          ),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
            decoration: BoxDecoration(
              color: badgeColor.withOpacity(0.15),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Text(
              badgeText,
              style: AppStyle.sm.copyWith(
                color: badgeColor,
                fontWeight: FontWeight.w700,
                fontSize: 12,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildDivider() {
    return Divider(
      height: 1,
      thickness: 0.5,
      color: AppColor.border.withOpacity(0.5),
    );
  }

  _MarginStatus _getMarginStatus(double hppPercentage) {
    if (hppPercentage <= 50) {
      return _MarginStatus('Sehat', AppColor.success);
    } else {
      return _MarginStatus('Tidak sehat', AppColor.error);
    }
  }
}

class _MarginStatus {
  final String label;
  final Color color;
  _MarginStatus(this.label, this.color);
}
