import 'package:equatable/equatable.dart';

class Credential extends Equatable {
  final String id;
  final String userId;
  final String credentialId;
  final String publicKey;
  final int signCount;
  final DateTime createdAt;
  final DateTime? lastUsedAt;
  final List<String>? transports;

  const Credential({
    required this.id,
    required this.userId,
    required this.credentialId,
    required this.publicKey,
    this.signCount = 0,
    required this.createdAt,
    this.lastUsedAt,
    this.transports,
  });

  @override
  List<Object?> get props => [
    id,
    userId,
    credentialId,
    publicKey,
    signCount,
    createdAt,
    lastUsedAt,
    transports,
  ];

  factory Credential.fromJson(Map<String, dynamic> json) {
    return Credential(
      id: json['id'] as String,
      userId: json['userId'] as String,
      credentialId: json['credentialId'] as String,
      publicKey: json['publicKey'] as String,
      signCount: json['signCount'] as int? ?? 0,
      createdAt: DateTime.parse(json['createdAt'] as String),
      lastUsedAt: json['lastUsedAt'] != null
          ? DateTime.parse(json['lastUsedAt'] as String)
          : null,
      transports: (json['transports'] as List<dynamic>?)?.cast<String>(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'credentialId': credentialId,
      'publicKey': publicKey,
      'signCount': signCount,
      'createdAt': createdAt.toIso8601String(),
      'lastUsedAt': lastUsedAt?.toIso8601String(),
      'transports': transports,
    };
  }
}
