// ============================
// PurchaseInvoiceOverviewModel.dart
// ============================

class PurchaseInvoiceOverviewResponse {
  final bool? success;
  final String? message;
  final PurchaseInvoiceOverviewData? data;
  final dynamic errors;
  final int? statusCode;
  final DateTime? timestamp;

  PurchaseInvoiceOverviewResponse({
    this.success,
    this.message,
    this.data,
    this.errors,
    this.statusCode,
    this.timestamp,
  });

  factory PurchaseInvoiceOverviewResponse.fromJson(Map<String, dynamic> json) {
    return PurchaseInvoiceOverviewResponse(
      success: json['success'] as bool?,
      message: json['message'] as String?,
      data: json['data'] != null
          ? PurchaseInvoiceOverviewData.fromJson(json['data'])
          : null,
      errors: json['errors'],
      statusCode: json['statusCode'] as int?,
      timestamp: json['timestamp'] != null
          ? DateTime.tryParse(json['timestamp'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'success': success,
      'message': message,
      'data': data?.toJson(),
      'errors': errors,
      'statusCode': statusCode,
      'timestamp': timestamp?.toIso8601String(),
    };
  }
}

class PurchaseInvoiceOverviewData {
  final int? totalInvoices;
  final double? totalValue;
  final double? averageValue;
  final List<StatusBreakdown>? statusBreakdown;
  final List<ProjectBreakdown>? projectBreakdown;
  final TopSupplier? topSupplier;

  PurchaseInvoiceOverviewData({
    this.totalInvoices,
    this.totalValue,
    this.averageValue,
    this.statusBreakdown,
    this.projectBreakdown,
    this.topSupplier,
  });

  factory PurchaseInvoiceOverviewData.fromJson(Map<String, dynamic> json) {
    return PurchaseInvoiceOverviewData(
      totalInvoices: json['totalInvoices'] as int?,
      totalValue: (json['totalValue'] != null)
          ? (json['totalValue'] as num).toDouble()
          : null,
      averageValue: (json['averageValue'] != null)
          ? (json['averageValue'] as num).toDouble()
          : null,
      statusBreakdown: json['statusBreakdown'] != null
          ? (json['statusBreakdown'] as List)
              .map((e) => StatusBreakdown.fromJson(e))
              .toList()
          : null,
      projectBreakdown: json['projectBreakdown'] != null
          ? (json['projectBreakdown'] as List)
              .map((e) => ProjectBreakdown.fromJson(e))
              .toList()
          : null,
      topSupplier: json['topSupplier'] != null
          ? TopSupplier.fromJson(json['topSupplier'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'totalInvoices': totalInvoices,
      'totalValue': totalValue,
      'averageValue': averageValue,
      'statusBreakdown': statusBreakdown?.map((e) => e.toJson()).toList(),
      'projectBreakdown': projectBreakdown?.map((e) => e.toJson()).toList(),
      'topSupplier': topSupplier?.toJson(),
    };
  }
}

class StatusBreakdown {
  final String? id;
  final String? name;
  final int? count;
  final double? totalValue;
  final double? percentage;

  StatusBreakdown({
    this.id,
    this.name,
    this.count,
    this.totalValue,
    this.percentage,
  });

  factory StatusBreakdown.fromJson(Map<String, dynamic> json) {
    return StatusBreakdown(
      id: json['id'] as String?,
      name: json['name'] as String?,
      count: json['count'] as int?,
      totalValue: (json['totalValue'] != null)
          ? (json['totalValue'] as num).toDouble()
          : null,
      percentage: (json['percentage'] != null)
          ? (json['percentage'] as num).toDouble()
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'count': count,
      'totalValue': totalValue,
      'percentage': percentage,
    };
  }
}

class ProjectBreakdown {
  final String? id;
  final String? name;
  final int? count;
  final double? totalValue;
  final double? percentage;

  ProjectBreakdown({
    this.id,
    this.name,
    this.count,
    this.totalValue,
    this.percentage,
  });

  factory ProjectBreakdown.fromJson(Map<String, dynamic> json) {
    return ProjectBreakdown(
      id: json['id'] as String?,
      name: json['name'] as String?,
      count: json['count'] as int?,
      totalValue: (json['totalValue'] != null)
          ? (json['totalValue'] as num).toDouble()
          : null,
      percentage: (json['percentage'] != null)
          ? (json['percentage'] as num).toDouble()
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'count': count,
      'totalValue': totalValue,
      'percentage': percentage,
    };
  }
}

class TopSupplier {
  final String? id;
  final String? name;
  final int? count;
  final double? totalValue;
  final double? percentage;

  TopSupplier({
    this.id,
    this.name,
    this.count,
    this.totalValue,
    this.percentage,
  });

  factory TopSupplier.fromJson(Map<String, dynamic> json) {
    return TopSupplier(
      id: json['id'] as String?,
      name: json['name'] as String?,
      count: json['count'] as int?,
      totalValue: (json['totalValue'] != null)
          ? (json['totalValue'] as num).toDouble()
          : null,
      percentage: (json['percentage'] != null)
          ? (json['percentage'] as num).toDouble()
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'count': count,
      'totalValue': totalValue,
      'percentage': percentage,
    };
  }
}
