import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:krow_domain/krow_domain.dart';
import 'package:lucide_icons/lucide_icons.dart';
// ignore: depend_on_referenced_packages
import 'package:core_localization/core_localization.dart';

class DocumentCard extends StatelessWidget {
  final StaffDocument document;
  final VoidCallback? onTap;

  const DocumentCard({
    super.key,
    required this.document,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: UiColors.bgPopup,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: UiColors.border),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: UiColors.primary.withOpacity(0.1),
              borderRadius: BorderRadius.circular(8),
            ),
            child: const Center(
              child: Icon(
                UiIcons.file,
                color: UiColors.primary,
                size: 20,
              ),
            ),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      document.name,
                      style: UiTypography.body1m.copyWith(
                        color: UiColors.textPrimary,
                      ),
                    ),
                    _getStatusIcon(document.status),
                  ],
                ),
                const SizedBox(height: 2),
                if (document.description != null)
                  Text(
                    document.description!,
                    style: UiTypography.body2r.copyWith(
                      color: UiColors.textSecondary,
                    ),
                  ),
                const SizedBox(height: 12),
                Row(
                  children: <Widget>[
                    _buildStatusBadge(document.status),
                    const SizedBox(width: 8),
                    _buildActionButton(document.status),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _getStatusIcon(DocumentStatus status) {
    switch (status) {
      case DocumentStatus.verified:
        return const Icon(
          UiIcons.check,
          color: UiColors.iconSuccess,
          size: 20,
        );
      case DocumentStatus.pending:
        return const Icon(
          UiIcons.clock,
          color: UiColors.textWarning,
          size: 20,
        );
      default:
        return const Icon(
          UiIcons.warning,
          color: UiColors.textError,
          size: 20,
        );
    }
  }

  Widget _buildStatusBadge(DocumentStatus status) {
    Color bg;
    Color text;
    String label;

    switch (status) {
      case DocumentStatus.verified:
        bg = UiColors.textSuccess.withOpacity(0.2);
        text = UiColors.textSuccess;
        label = t.staff_documents.card.verified;
        break;
      case DocumentStatus.pending:
        bg = UiColors.textWarning.withOpacity(0.2);
        text = UiColors.textWarning;
        label = t.staff_documents.card.pending;
        break;
      case DocumentStatus.missing:
        bg = UiColors.textError.withOpacity(0.2);
        text = UiColors.textError;
        label = t.staff_documents.card.missing;
        break;
      case DocumentStatus.rejected:
        bg = UiColors.textError.withOpacity(0.2);
        text = UiColors.textError;
        label = t.staff_documents.card.rejected;
        break;
      case DocumentStatus.expired:
        bg = UiColors.textError.withOpacity(0.2);
        text = UiColors.textError;
        label = t.staff_documents.card.rejected; // Or define "Expired" string
        break;
    }

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
      decoration: BoxDecoration(
        color: bg,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Text(
        label,
        style: UiTypography.body3m.copyWith(
          color: text,
        ),
      ),
    );
  }

  Widget _buildActionButton(DocumentStatus status) {
    final bool isVerified = status == DocumentStatus.verified;
    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(4),
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
        child: Row(
          children: <Widget>[
            Icon(
              isVerified ? UiIcons.eye : LucideIcons.upload,
              size: 16,
              color: UiColors.primary,
            ),
            const SizedBox(width: 4),
            Text(
              isVerified
                  ? t.staff_documents.card.view
                  : t.staff_documents.card.upload,
              style: UiTypography.body3m.copyWith(
                color: UiColors.primary,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
