import '../../entities/profile/tax_form.dart';

/// Adapter for [TaxForm] to map data layer values to domain entity.
class TaxFormAdapter {
  /// Maps primitive values to [TaxForm].
  static TaxForm fromPrimitives({
    required String id,
    required String type,
    required String title,
    String? subtitle,
    String? description,
    required String status,
    String? staffId,
    dynamic formData,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    final TaxFormType formType = _stringToType(type);
    final TaxFormStatus formStatus = _stringToStatus(status);
    final Map<String, dynamic> formDetails =
        formData is Map ? Map<String, dynamic>.from(formData as Map) : <String, dynamic>{};

    if (formType == TaxFormType.i9) {
      return I9TaxForm(
        id: id,
        title: title,
        subtitle: subtitle,
        description: description,
        status: formStatus,
        staffId: staffId,
        formData: formDetails,
        createdAt: createdAt,
        updatedAt: updatedAt,
      );
    } else {
      return W4TaxForm(
        id: id,
        title: title,
        subtitle: subtitle,
        description: description,
        status: formStatus,
        staffId: staffId,
        formData: formDetails,
        createdAt: createdAt,
        updatedAt: updatedAt,
      );
    }
  }

  static TaxFormType _stringToType(String? value) {
    if (value == null) return TaxFormType.i9;
    try {
      return TaxFormType.values.firstWhere(
        (TaxFormType e) => e.name.toLowerCase() == value.toLowerCase(),
        orElse: () => TaxFormType.i9,
      );
    } catch (_) {
      return TaxFormType.i9;
    }
  }

  static TaxFormStatus _stringToStatus(String? value) {
    if (value == null) return TaxFormStatus.notStarted;
    try {
      final String normalizedValue = value.replaceAll('_', '').toLowerCase();
      // map DRAFT to inProgress
      if (normalizedValue == 'draft') return TaxFormStatus.inProgress;

      return TaxFormStatus.values.firstWhere(
        (TaxFormStatus e) {
            // Handle differences like not_started vs notStarted if any, 
            // but standardizing to lowercase is a good start. 
            // The enum names are camelCase in Dart, but might be SNAKE_CASE from backend.
           final String normalizedEnum = e.name.toLowerCase();
           return normalizedValue == normalizedEnum;
        },
        orElse: () => TaxFormStatus.notStarted,
      );
    } catch (_) {
      return TaxFormStatus.notStarted;
    }
  }

  /// Converts domain [TaxFormType] to string for backend.
  static String typeToString(TaxFormType type) {
    return type.name.toUpperCase();
  }

  /// Converts domain [TaxFormStatus] to string for backend.
  static String statusToString(TaxFormStatus status) {
    switch (status) {
      case TaxFormStatus.notStarted:
        return 'NOT_STARTED';
      case TaxFormStatus.inProgress:
        return 'DRAFT'; 
      case TaxFormStatus.submitted:
        return 'SUBMITTED';
      case TaxFormStatus.approved:
        return 'APPROVED';
      case TaxFormStatus.rejected:
        return 'REJECTED';
    }
  }
}
