import 'package:intl/intl.dart';

class RecentScanHistoryResponse {
  final List<RecentScanHistory> data;

  RecentScanHistoryResponse({
    required this.data,
  });

  factory RecentScanHistoryResponse.fromJson(Map<String, dynamic> json) {
    return RecentScanHistoryResponse(
      data: (json['data'] as List<dynamic>?)
              ?.map((e) => RecentScanHistory.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }
}

class RecentScanHistory {
  final int id;
  final String passId;
  final String qrCode;
  final String bookingNumber;
  final int attractionId;
  final String attractionTitle;
  final String customerName;
  final String customerEmail;
  final String cardType;
  final int? scannedByPartnerStaffId;
  final String scannedByPartnerStaffName;
  final String status;
  final String reason;
  final DateTime? checkedInDatetime;
  final DateTime? activatedAt;
  final DateTime? qrExpiresAt;
  final DateTime? validUpto;
  final DateTime? createdAt;

  RecentScanHistory({
    required this.id,
    required this.passId,
    required this.qrCode,
    required this.bookingNumber,
    required this.attractionId,
    required this.attractionTitle,
    required this.customerName,
    required this.customerEmail,
    required this.cardType,
    this.scannedByPartnerStaffId,
    required this.scannedByPartnerStaffName,
    required this.status,
    required this.reason,
    this.checkedInDatetime,
    this.activatedAt,
    this.qrExpiresAt,
    this.validUpto,
    this.createdAt,
  });

  factory RecentScanHistory.fromJson(Map<String, dynamic> json) {
    return RecentScanHistory(
      id: json['id'] ?? 0,
      passId: (json['passId'] ?? '').toString(),
      qrCode: (json['qrCode'] ?? '').toString(),
      bookingNumber: (json['bookingNumber'] ?? '').toString(),
      attractionId: json['attractionId'] ?? 0,
      attractionTitle: (json['attractionTitle'] ?? '').toString(),
      customerName: (json['customerName'] ?? '').toString(),
      customerEmail: (json['customerEmail'] ?? '').toString(),
      cardType: (json['cardType'] ?? '').toString(),
      scannedByPartnerStaffId: json['scannedByPartnerStaffId'],
      scannedByPartnerStaffName:
          (json['scannedByPartnerStaffName'] ?? '').toString(),
      status: (json['status'] ?? '').toString(),
      reason: (json['reason'] ?? '').toString(),
      checkedInDatetime: _parseDate(json['checkedInDatetime']),
      activatedAt: _parseDate(json['activatedAt']),
      qrExpiresAt: _parseDate(json['qrExpiresAt']),
      validUpto: _parseDate(json['validUpto']),
      createdAt: _parseDate(json['createdAt']),
    );
  }

  static DateTime? _parseDate(dynamic date) {
    if (date == null) return null;
    try {
      return DateTime.parse(date.toString());
    } catch (e) {
      return null;
    }
  }

  static final DateFormat _formatter = DateFormat('dd MMM yyyy, hh:mm a');

  String formatTime(DateTime? date) {
    if (date == null) return 'N/A';
    return _formatter.format(date);
  }
}
