import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:krow_domain/krow_domain.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: UiConstants.space3),
      padding: const EdgeInsets.all(UiConstants.space4),
      decoration: BoxDecoration(
        color: UiColors.bgPopup,
        borderRadius: UiConstants.radiusLg,
        border: Border.all(color: UiColors.border),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: UiColors.primary.withValues(alpha: 0.1),
              borderRadius: BorderRadius.circular(UiConstants.space2),
            ),
            child: const Center(
              child: Icon(
                UiIcons.file,
                color: UiColors.primary,
                size: 20,
              ),
            ),
          ),
          const SizedBox(width: UiConstants.space3),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      document.name,
                      style: UiTypography.body1m.textPrimary,
                    ),
                    _getStatusIcon(document.status),
                  ],
                ),
                const SizedBox(height: UiConstants.space1 / 2),
                if (document.description != null)
                  Text(
                    document.description!,
                    style: UiTypography.body2r.textSecondary,
                  ),
                const SizedBox(height: UiConstants.space3),
                Row(
                  children: <Widget>[
                    _buildStatusBadge(document.status),
                    const SizedBox(width: UiConstants.space2),
                    _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.tagSuccess;
        text = UiColors.textSuccess;
        label = t.staff_documents.card.verified;
        break;
      case DocumentStatus.pending:
        bg = UiColors.tagPending;
        text = UiColors.textWarning;
        label = t.staff_documents.card.pending;
        break;
      case DocumentStatus.missing:
        bg = UiColors.textError.withValues(alpha: 0.1);
        text = UiColors.textError;
        label = t.staff_documents.card.missing;
        break;
      case DocumentStatus.rejected:
        bg = UiColors.textError.withValues(alpha: 0.1);
        text = UiColors.textError;
        label = t.staff_documents.card.rejected;
        break;
      case DocumentStatus.expired:
        bg = UiColors.textError.withValues(alpha: 0.1);
        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: UiConstants.radiusFull,
      ),
      child: Text(
        label,
        style: UiTypography.body3m.copyWith(
          color: text,
        ),
      ),
    );
  }

  Widget _buildActionButton(DocumentStatus status) {
    final bool isVerified = status == DocumentStatus.verified;
    return InkWell(
      onTap: onTap,
      borderRadius: UiConstants.radiusSm,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
        child: Row(
          children: <Widget>[
            Icon(
              isVerified ? UiIcons.eye : UiIcons.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.primary,
            ),
          ],
        ),
      ),
    );
  }
}
