class License {
  final int? id;
  final String licenseKey;
  final String? productName;
  final String? vendor;
  final String? licenseType;
  final int? userCount;
  final DateTime? purchaseDate;
  final DateTime? expiryDate;
  final double? purchasePrice;
  final int? companyId;
  final int? branchId;
  final int? assignedUserId;
  final String? remark;
  final bool isActive;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  
  // 조인된 데이터 필드
  final String? companyName;
  final String? branchName;
  final String? assignedUserName;
  
  // 계산 필드
  int? get daysUntilExpiry {
    if (expiryDate == null) return null;
    return expiryDate!.difference(DateTime.now()).inDays;
  }
  
  bool get isExpired {
    if (expiryDate == null) return false;
    return expiryDate!.isBefore(DateTime.now());
  }
  
  String get status {
    if (!isActive) return 'inactive';
    if (isExpired) return 'expired';
    if (daysUntilExpiry != null && daysUntilExpiry! <= 30) return 'expiring';
    return 'active';
  }

  License({
    this.id,
    required this.licenseKey,
    this.productName,
    this.vendor,
    this.licenseType,
    this.userCount,
    this.purchaseDate,
    this.expiryDate,
    this.purchasePrice,
    this.companyId,
    this.branchId,
    this.assignedUserId,
    this.remark,
    this.isActive = true,
    this.createdAt,
    this.updatedAt,
    this.companyName,
    this.branchName,
    this.assignedUserName,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'license_key': licenseKey,
      'product_name': productName,
      'vendor': vendor,
      'license_type': licenseType,
      'user_count': userCount,
      'purchase_date': purchaseDate?.toIso8601String(),
      'expiry_date': expiryDate?.toIso8601String(),
      'purchase_price': purchasePrice,
      'company_id': companyId,
      'branch_id': branchId,
      'assigned_user_id': assignedUserId,
      'remark': remark,
      'is_active': isActive,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  factory License.fromJson(Map<String, dynamic> json) {
    return License(
      id: json['id'] as int?,
      licenseKey: json['license_key'] as String,
      productName: json['product_name'] as String?,
      vendor: json['vendor'] as String?,
      licenseType: json['license_type'] as String?,
      userCount: json['user_count'] as int?,
      purchaseDate: json['purchase_date'] != null 
        ? DateTime.parse(json['purchase_date'] as String) : null,
      expiryDate: json['expiry_date'] != null
        ? DateTime.parse(json['expiry_date'] as String) : null,
      purchasePrice: (json['purchase_price'] as num?)?.toDouble(),
      companyId: json['company_id'] as int?,
      branchId: json['branch_id'] as int?,
      assignedUserId: json['assigned_user_id'] as int?,
      remark: json['remark'] as String?,
      isActive: json['is_active'] ?? true,
      createdAt: json['created_at'] != null
        ? DateTime.parse(json['created_at'] as String) : null,
      updatedAt: json['updated_at'] != null
        ? DateTime.parse(json['updated_at'] as String) : null,
      companyName: json['company_name'] as String?,
      branchName: json['branch_name'] as String?,
      assignedUserName: json['assigned_user_name'] as String?,
    );
  }
  
  License copyWith({
    int? id,
    String? licenseKey,
    String? productName,
    String? vendor,
    String? licenseType,
    int? userCount,
    DateTime? purchaseDate,
    DateTime? expiryDate,
    double? purchasePrice,
    int? companyId,
    int? branchId,
    int? assignedUserId,
    String? remark,
    bool? isActive,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? companyName,
    String? branchName,
    String? assignedUserName,
  }) {
    return License(
      id: id ?? this.id,
      licenseKey: licenseKey ?? this.licenseKey,
      productName: productName ?? this.productName,
      vendor: vendor ?? this.vendor,
      licenseType: licenseType ?? this.licenseType,
      userCount: userCount ?? this.userCount,
      purchaseDate: purchaseDate ?? this.purchaseDate,
      expiryDate: expiryDate ?? this.expiryDate,
      purchasePrice: purchasePrice ?? this.purchasePrice,
      companyId: companyId ?? this.companyId,
      branchId: branchId ?? this.branchId,
      assignedUserId: assignedUserId ?? this.assignedUserId,
      remark: remark ?? this.remark,
      isActive: isActive ?? this.isActive,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      companyName: companyName ?? this.companyName,
      branchName: branchName ?? this.branchName,
      assignedUserName: assignedUserName ?? this.assignedUserName,
    );
  }
  
  // Mock 데이터 호환을 위한 추가 getter (기존 코드 호환)
  String get name => productName ?? licenseKey;
  int get durationMonths => 12; // 기본값
  String get visitCycle => '월'; // 기본값
}
