import '../../domain/entities/compliance_snapshot.dart';

class ComplianceSnapshotModel extends ComplianceSnapshot {
  const ComplianceSnapshotModel({
    required super.organisationId,
    required super.organisationNom,
    required super.referentielComptable,
    required super.complianceOfficerDesigne,
    required super.agAnnuelle,
    required super.rapportAirms,
    required super.dirigeantsAvecCmu,
    required super.tauxKycAJourPct,
    required super.tauxFormationLbcFtPct,
    required super.commissaireAuxComptes,
    required super.fomusCi,
    required super.couvertureUboPct,
    required super.scoreGlobal,
  });

  factory ComplianceSnapshotModel.fromJson(Map<String, dynamic> json) {
    return ComplianceSnapshotModel(
      organisationId: json['organisationId']?.toString() ?? '',
      organisationNom: json['organisationNom']?.toString() ?? '',
      referentielComptable: json['referentielComptable']?.toString() ?? 'SYSCOHADA',
      complianceOfficerDesigne: json['complianceOfficerDesigne'] as bool? ?? false,
      agAnnuelle: _indicateur(json['agAnnuelle']),
      rapportAirms: _indicateur(json['rapportAirms']),
      dirigeantsAvecCmu: (json['dirigeantsAvecCmu'] as num?)?.toInt() ?? 0,
      tauxKycAJourPct: _toDouble(json['tauxKycAJourPct']),
      tauxFormationLbcFtPct: _toDouble(json['tauxFormationLbcFtPct']),
      commissaireAuxComptes: _indicateur(json['commissaireAuxComptes']),
      fomusCi: _indicateur(json['fomusCi']),
      couvertureUboPct: _toDouble(json['couvertureUboPct']),
      scoreGlobal: (json['scoreGlobal'] as num?)?.toInt() ?? 0,
    );
  }

  static ConformiteIndicateur _indicateur(dynamic raw) {
    if (raw is Map<String, dynamic>) {
      return ConformiteIndicateur(
        statut: raw['statut']?.toString() ?? 'EN_VEILLE',
        message: raw['message']?.toString() ?? '',
      );
    }
    return const ConformiteIndicateur(statut: 'EN_VEILLE', message: '');
  }

  static double _toDouble(dynamic raw) {
    if (raw is num) return raw.toDouble();
    if (raw is String) return double.tryParse(raw) ?? 0.0;
    return 0.0;
  }
}
