import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../../../shared/design_system/tokens/color_tokens.dart';
import '../../../../shared/design_system/tokens/module_colors.dart';
import '../../../members/data/models/membre_complete_model.dart';

/// Widget d'affichage du statut KYC (Know Your Customer) d'un membre.
/// Affiche en lecture seule le niveau de vigilance, le statut de vérification,
/// et la date de vérification d'identité (conformité LCB-FT).
class KycStatusWidget extends StatelessWidget {
  final NiveauVigilanceKyc? niveauVigilance;
  final StatutKyc? statutKyc;
  final DateTime? dateVerification;

  const KycStatusWidget({
    super.key,
    this.niveauVigilance,
    this.statutKyc,
    this.dateVerification,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;

    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Icon(
                  Icons.verified_user,
                  color: ModuleColors.profil,
                  size: 24,
                ),
                const SizedBox(width: 8),
                Text(
                  'Vérification KYC (Anti-blanchiment)',
                  style: theme.textTheme.titleMedium?.copyWith(
                    fontWeight: FontWeight.bold,
                    color: ModuleColors.profil,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 4),
            Text(
              'Conformité LCB-FT (Lutte contre le Blanchiment)',
              style: theme.textTheme.bodySmall?.copyWith(
                color: colorScheme.onSurfaceVariant,
                fontStyle: FontStyle.italic,
              ),
            ),
            const Divider(height: 24),
            _buildInfoRow(
              context,
              'Statut de vérification',
              _getStatutKycLabel(statutKyc),
              _getStatutKycColor(statutKyc),
            ),
            const SizedBox(height: 12),
            _buildInfoRow(
              context,
              'Niveau de vigilance',
              _getNiveauVigilanceLabel(niveauVigilance),
              _getNiveauVigilanceColor(niveauVigilance),
            ),
            if (dateVerification != null) ...[
              const SizedBox(height: 12),
              _buildInfoRow(
                context,
                'Date de vérification',
                DateFormat('dd/MM/yyyy').format(dateVerification!),
                colorScheme.onSurface,
              ),
            ],
            const SizedBox(height: 16),
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: ModuleColors.profil.withOpacity(0.08),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Row(
                children: [
                  Icon(
                    Icons.info_outline,
                    size: 16,
                    color: ModuleColors.profil,
                  ),
                  const SizedBox(width: 8),
                  Expanded(
                    child: Text(
                      'Ces informations sont gérées par l\'administrateur et permettent de garantir la conformité aux normes BCEAO/OHADA.',
                      style: theme.textTheme.bodySmall,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(
    BuildContext context,
    String label,
    String value,
    Color valueColor,
  ) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(
          flex: 2,
          child: Text(
            label,
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                  fontWeight: FontWeight.w500,
                ),
          ),
        ),
        Expanded(
          flex: 3,
          child: Text(
            value,
            style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                  color: valueColor,
                  fontWeight: FontWeight.bold,
                ),
          ),
        ),
      ],
    );
  }

  String _getStatutKycLabel(StatutKyc? statut) {
    if (statut == null) return 'Non renseigné';
    switch (statut) {
      case StatutKyc.nonVerifie:
        return '⏸️ Non vérifié';
      case StatutKyc.enCours:
        return '⏳ En cours de vérification';
      case StatutKyc.verifie:
        return '✅ Vérifié';
      case StatutKyc.refuse:
        return '❌ Refusé';
    }
  }

  Color _getStatutKycColor(StatutKyc? statut) {
    if (statut == null) return ColorTokens.textSecondary;
    switch (statut) {
      case StatutKyc.nonVerifie:
        return ColorTokens.warning;
      case StatutKyc.enCours:
        return ColorTokens.info;
      case StatutKyc.verifie:
        return ColorTokens.success;
      case StatutKyc.refuse:
        return ColorTokens.error;
    }
  }

  String _getNiveauVigilanceLabel(NiveauVigilanceKyc? niveau) {
    if (niveau == null) return 'Non renseigné';
    switch (niveau) {
      case NiveauVigilanceKyc.simplifie:
        return '🔵 Simplifiée';
      case NiveauVigilanceKyc.renforce:
        return '🔴 Renforcée';
    }
  }

  Color _getNiveauVigilanceColor(NiveauVigilanceKyc? niveau) {
    if (niveau == null) return ColorTokens.textSecondary;
    switch (niveau) {
      case NiveauVigilanceKyc.simplifie:
        return ColorTokens.info;
      case NiveauVigilanceKyc.renforce:
        return ColorTokens.warning;
    }
  }
}
