import 'package:flutter/material.dart';
import '../../core/theme/app_colors_extension.dart';
import '../../core/i18n/app_localizations.dart';

/// 审批状态标签，深色 / 浅色主题自适应。
class StatusTag extends StatelessWidget {
  /// 状态原始值（pending / approved / rejected / draft / withdrawn / returned / completed）。
  final String status;

  /// 可选：自定义标签文本，不传则根据 [status] 自动查 i18n。
  final String? text;

  /// 可选：是否是付款状态标签（approved 时区分 paid / unpaid）。
  final String? paymentStatus;

  final double borderRadius;
  final EdgeInsets padding;

  const StatusTag({
    super.key,
    required this.status,
    this.text,
    this.paymentStatus,
    this.borderRadius = 4,
    this.padding = const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
  });

  factory StatusTag.fromStatus(String status, AppLocalizations l10n) {
    return StatusTag(status: status, text: _statusLabel(status, l10n));
  }

  factory StatusTag.fromPaymentStatus(
    String status,
    String paymentStatus,
    AppLocalizations l10n,
  ) {
    if (status == 'approved') {
      if (paymentStatus == 'paid') {
        return StatusTag(
          status: status,
          text: l10n.get('paid'),
          paymentStatus: paymentStatus,
        );
      }
      return StatusTag(
        status: status,
        text: l10n.get('statusWaitPay'),
        paymentStatus: paymentStatus,
      );
    }
    return StatusTag(status: status, text: _statusLabel(status, l10n));
  }

  static String _statusLabel(String s, AppLocalizations l10n) {
    switch (s) {
      case 'pending':
        return l10n.get('statusPending');
      case 'approved':
        return l10n.get('statusApproved');
      case 'rejected':
        return l10n.get('statusRejected');
      case 'draft':
        return l10n.get('statusDraft');
      case 'withdrawn':
        return l10n.get('statusWithdrawn');
      case 'returned':
        return l10n.get('returned');
      case 'completed':
        return l10n.get('completed');
      default:
        return s;
    }
  }

  @override
  Widget build(BuildContext context) {
    final colors = Theme.of(context).extension<AppColorsExtension>()!;
    final (bg, fg) = _statusColors(colors);
    return Container(
      padding: padding,
      decoration: BoxDecoration(
        color: bg,
        borderRadius: BorderRadius.circular(borderRadius),
      ),
      child: Text(
        text ?? status,
        style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500, color: fg),
      ),
    );
  }

  (Color, Color) _statusColors(AppColorsExtension c) {
    if (status == 'approved' && paymentStatus == 'paid') {
      return (c.paidBg, c.paidText);
    }
    if (status == 'approved' && paymentStatus == 'unpaid') {
      return (c.unpaidBg, c.unpaidText);
    }
    switch (status) {
      case 'pending':
        return (c.warningBg, c.warning);
      case 'approved':
        return (c.successBg, c.success);
      case 'rejected':
        return (c.dangerBg, c.danger);
      case 'draft':
        return (c.bgDisabled, c.statusGray);
      case 'withdrawn':
        return (c.withdrawnBg, c.withdrawnText);
      case 'returned':
        return (c.infoBg, c.infoText);
      case 'completed':
        return (c.successBg, c.success);
      default:
        return (c.bgPage, c.statusGray);
    }
  }
}
