/// Model representing an application demo request
class ApplicationDemoRequestModel {
  final String? id;
  final String fullName;
  final String emailAddress;
  final String mobileNumber;
  final String? companyName;

  // final String? companyName;
  final List<String> roleIds;
  final List<String> roleNames;
  final String? status; // Pending, Approved, Rejected
  final DateTime requestDate;

  ApplicationDemoRequestModel({
    this.id,
    required this.fullName,
    required this.emailAddress,
    required this.mobileNumber,
    this.companyName,
    // this.companyId,
    required this.roleIds,
    required this.roleNames,
    this.status,
    required this.requestDate,
  });

  /// Converts model to JSON for API submission
  Map<String, dynamic> toJson() {
    return {
      'id': id ?? 0,
      'fullName': fullName,
      'emailAddress': emailAddress,
      'mobileNumber': mobileNumber,
      'companyName': companyName,
      // 'companyId':  88,
      'demoRequestUserRoleIds': roleIds,
    };
  }

  /// Creates model from API response JSON
  factory ApplicationDemoRequestModel.fromJson(Map<String, dynamic> json) {
    return ApplicationDemoRequestModel(
      id: json['id']?.toString() ?? json['requestNumber']?.toString() ?? '',
      fullName: json['fullName'] ?? '',
      emailAddress: json['emailAddress'] ?? json['email'] ?? '',
      mobileNumber: json['mobileNumber'] ?? json['mobile'] ?? '',
      companyName: json['companyName'] ?? json['company'] ?? '',
      // companyId: json['companyId'],
      roleIds: _parseList(json['roleIds'] ?? json['demoRequestUserRoleIds']),
      roleNames: _parseList(json['roleNames'] ?? json['roles']),
      status: json['status'] ?? 'Pending',
      requestDate: _parseDate(json),
    );
  }

  /// Helper method to parse list from various formats
  static List<String> _parseList(dynamic value) {
    if (value == null) return [];

    if (value is List) {
      return value.map((e) => e.toString()).toList();
    }

    if (value is String) {
      if (value.isEmpty) return [];
      return value.split(',').map((e) => e.trim()).where((e) => e.isNotEmpty).toList();
    }

    return [];
  }

  /// Helper method to parse date from various formats
  static DateTime _parseDate(Map<String, dynamic> json) {
    final dateValue = json['requestDate'] ?? json['createdDate'] ?? json['date'];

    if (dateValue == null) return DateTime.now();

    if (dateValue is String) {
      try {
        return DateTime.parse(dateValue);
      } catch (e) {
        return DateTime.now();
      }
    }

    if (dateValue is DateTime) return dateValue;

    return DateTime.now();
  }

  /// Creates a copy with optional field updates
  ApplicationDemoRequestModel copyWith({
    String? id,
    String? fullName,
    String? emailAddress,
    String? mobileNumber,
    String? companyName,
    int? companyId,
    List<String>? roleIds,
    List<String>? roleNames,
    String? status,
    DateTime? requestDate,
  }) {
    return ApplicationDemoRequestModel(
      id: id ?? this.id,
      fullName: fullName ?? this.fullName,
      emailAddress: emailAddress ?? this.emailAddress,
      mobileNumber: mobileNumber ?? this.mobileNumber,
      companyName: companyName ?? this.companyName,
      // companyId: companyId ?? this.companyId,
      roleIds: roleIds ?? this.roleIds,
      roleNames: roleNames ?? this.roleNames,
      status: status ?? this.status,
      requestDate: requestDate ?? this.requestDate,
    );
  }
}
