class GlobalProjectModel {
  final String id;
  final String name;
  final String projectAddress;
  final String contactPerson;
  final DateTime? startDate;
  final DateTime? endDate;
  final int teamSize;
  final String projectStatusId;
  final String? tenantId;

  GlobalProjectModel({
    required this.id,
    required this.name,
    required this.projectAddress,
    required this.contactPerson,
    this.startDate,
    this.endDate,
    required this.teamSize,
    required this.projectStatusId,
    this.tenantId,
  });

  factory GlobalProjectModel.fromJson(Map<String, dynamic> json) {
    return GlobalProjectModel(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      projectAddress: json['projectAddress'] ?? '',
      contactPerson: json['contactPerson'] ?? '',
      startDate: _parseDate(json['startDate']),
      endDate: _parseDate(json['endDate']),
      teamSize: json['teamSize'] is int
          ? json['teamSize']
          : int.tryParse(json['teamSize']?.toString() ?? '0') ?? 0,
      projectStatusId: json['projectStatusId'] ?? '',
      tenantId: json['tenantId'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'projectAddress': projectAddress,
      'contactPerson': contactPerson,
      'startDate': startDate?.toIso8601String(),
      'endDate': endDate?.toIso8601String(),
      'teamSize': teamSize,
      'projectStatusId': projectStatusId,
      'tenantId': tenantId,
    };
  }

  static DateTime? _parseDate(dynamic value) {
    if (value == null || value.toString().trim().isEmpty) {
      return null;
    }
    try {
      return DateTime.parse(value.toString());
    } catch (e) {
      print('⚠️ Failed to parse date "$value": $e');
      return null;
    }
  }
}
