import 'dart:convert';

/// ===============================
/// MAIN MODEL: CollectionOverview
/// ===============================

class CollectionOverviewResponse {
  final bool success;
  final String message;
  final CollectionOverviewData data;
  final dynamic errors;
  final int statusCode;
  final DateTime timestamp;

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

  factory CollectionOverviewResponse.fromJson(Map<String, dynamic> json) {
    return CollectionOverviewResponse(
      success: json['success'] ?? false,
      message: json['message'] ?? '',
      data: CollectionOverviewData.fromJson(json['data'] ?? {}),
      errors: json['errors'],
      statusCode: json['statusCode'] ?? 0,
      timestamp: DateTime.tryParse(json['timestamp'] ?? '') ?? DateTime.now(),
    );
  }

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

/// ===============================
/// DATA BLOCK
/// ===============================

class CollectionOverviewData {
  final double totalDueAmount;
  final double totalCollectedAmount;
  final double totalValue;
  final double pendingPercentage;
  final double collectedPercentage;

  final int bucket0To30Invoices;
  final int bucket30To60Invoices;
  final int bucket60To90Invoices;
  final int bucket90PlusInvoices;

  final double bucket0To30Amount;
  final double bucket30To60Amount;
  final double bucket60To90Amount;
  final double bucket90PlusAmount;

  final double topClientBalance;
  final TopClient? topClient;

  CollectionOverviewData({
    required this.totalDueAmount,
    required this.totalCollectedAmount,
    required this.totalValue,
    required this.pendingPercentage,
    required this.collectedPercentage,
    required this.bucket0To30Invoices,
    required this.bucket30To60Invoices,
    required this.bucket60To90Invoices,
    required this.bucket90PlusInvoices,
    required this.bucket0To30Amount,
    required this.bucket30To60Amount,
    required this.bucket60To90Amount,
    required this.bucket90PlusAmount,
    required this.topClientBalance,
    required this.topClient,
  });

  factory CollectionOverviewData.fromJson(Map<String, dynamic> json) {
    return CollectionOverviewData(
      totalDueAmount: (json['totalDueAmount'] ?? 0).toDouble(),
      totalCollectedAmount: (json['totalCollectedAmount'] ?? 0).toDouble(),
      totalValue: (json['totalValue'] ?? 0).toDouble(),
      pendingPercentage: (json['pendingPercentage'] ?? 0).toDouble(),
      collectedPercentage: (json['collectedPercentage'] ?? 0).toDouble(),

      bucket0To30Invoices: json['bucket0To30Invoices'] ?? 0,
      bucket30To60Invoices: json['bucket30To60Invoices'] ?? 0,
      bucket60To90Invoices: json['bucket60To90Invoices'] ?? 0,
      bucket90PlusInvoices: json['bucket90PlusInvoices'] ?? 0,

      bucket0To30Amount: (json['bucket0To30Amount'] ?? 0).toDouble(),
      bucket30To60Amount: (json['bucket30To60Amount'] ?? 0).toDouble(),
      bucket60To90Amount: (json['bucket60To90Amount'] ?? 0).toDouble(),
      bucket90PlusAmount: (json['bucket90PlusAmount'] ?? 0).toDouble(),

      topClientBalance: (json['topClientBalance'] ?? 0).toDouble(),
      topClient: json['topClient'] != null
          ? TopClient.fromJson(json['topClient'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'totalDueAmount': totalDueAmount,
      'totalCollectedAmount': totalCollectedAmount,
      'totalValue': totalValue,
      'pendingPercentage': pendingPercentage,
      'collectedPercentage': collectedPercentage,
      'bucket0To30Invoices': bucket0To30Invoices,
      'bucket30To60Invoices': bucket30To60Invoices,
      'bucket60To90Invoices': bucket60To90Invoices,
      'bucket90PlusInvoices': bucket90PlusInvoices,
      'bucket0To30Amount': bucket0To30Amount,
      'bucket30To60Amount': bucket30To60Amount,
      'bucket60To90Amount': bucket60To90Amount,
      'bucket90PlusAmount': bucket90PlusAmount,
      'topClientBalance': topClientBalance,
      'topClient': topClient?.toJson(),
    };
  }
}

/// ===============================
/// NESTED MODEL: Top Client
/// ===============================

class TopClient {
  final String id;
  final String name;
  final String? email;
  final String? contactPerson;
  final String? address;
  final String? gstNumber;
  final String? contactNumber;
  final int? sprid;

  TopClient({
    required this.id,
    required this.name,
    this.email,
    this.contactPerson,
    this.address,
    this.gstNumber,
    this.contactNumber,
    this.sprid,
  });

  factory TopClient.fromJson(Map<String, dynamic> json) {
    return TopClient(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      email: json['email'],
      contactPerson: json['contactPerson'],
      address: json['address'],
      gstNumber: json['gstNumber'],
      contactNumber: json['contactNumber'],
      sprid: json['sprid'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'email': email,
      'contactPerson': contactPerson,
      'address': address,
      'gstNumber': gstNumber,
      'contactNumber': contactNumber,
      'sprid': sprid,
    };
  }
}

/// ===============================
/// Optional: Quick decode method
/// ===============================
CollectionOverviewResponse parseCollectionOverview(String jsonString) {
  return CollectionOverviewResponse.fromJson(jsonDecode(jsonString));
}
