import 'package:flutter/material.dart';

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

/// Ambang kesehatan berdasarkan % HPP riil terhadap omzet.
/// Ubah di sini kalau target margin bisnis berubah.
class HppThreshold {
  static const double healthy = 70;
  static const double watch = 80;
}

String formatPercent(double value) => '${value.toStringAsFixed(1)}%';

Color statusColor(double realHppPercentage) {
  if (realHppPercentage <= HppThreshold.healthy) return AppColor.success;
  if (realHppPercentage <= HppThreshold.watch) return AppColor.warning;
  return AppColor.error;
}

String statusLabel(BuildContext context, double realHppPercentage) {
  if (realHppPercentage <= HppThreshold.healthy) {
    return context.lang.status_healthy;
  }
  if (realHppPercentage <= HppThreshold.watch) return context.lang.status_watch;
  return context.lang.status_critical;
}

/// Pill status margin (Sehat / Waspada / Tidak Sehat).
class ProfitSharingStatusBadge extends StatelessWidget {
  final double realHppPercentage;
  final bool onDarkBackground;

  const ProfitSharingStatusBadge({
    super.key,
    required this.realHppPercentage,
    this.onDarkBackground = false,
  });

  @override
  Widget build(BuildContext context) {
    final color = statusColor(realHppPercentage);

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
      decoration: BoxDecoration(
        color: onDarkBackground
            ? AppColor.textWhite.withOpacity(0.18)
            : color.withOpacity(0.12),
        borderRadius: BorderRadius.circular(20),
        border: onDarkBackground
            ? Border.all(color: AppColor.textWhite.withOpacity(0.3))
            : null,
      ),
      child: Text(
        statusLabel(context, realHppPercentage),
        style: AppStyle.xs.copyWith(
          color: onDarkBackground ? AppColor.textWhite : color,
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }
}

/// Label kecil dua baris: judul di atas, nilai di bawah.
class ProfitSharingMetric extends StatelessWidget {
  final String label;
  final String value;
  final Color? valueColor;
  final bool isBold;

  const ProfitSharingMetric({
    super.key,
    required this.label,
    required this.value,
    this.valueColor,
    this.isBold = false,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: AppStyle.xs.copyWith(color: AppColor.textSecondary),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        const SizedBox(height: 2),
        Text(
          value,
          style: AppStyle.sm.copyWith(
            color: valueColor ?? AppColor.textPrimary,
            fontWeight: isBold ? FontWeight.w800 : FontWeight.w600,
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ],
    );
  }
}
