import 'package:intl/intl.dart';

class ScanHistoryResponse {
  final List<ScanHistory> data;

  ScanHistoryResponse({
    required this.data,
  });

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

class ScanHistory {
  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; // ✅ ADDED
  final DateTime? checkedInDatetime;
  final DateTime? activatedAt;
  final DateTime? qrExpiresAt;
  final DateTime? validUpto;
  final DateTime? createdAt;

  ScanHistory({
    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, // ✅ ADDED
    this.checkedInDatetime,
    this.activatedAt,
    this.qrExpiresAt,
    this.validUpto,
    this.createdAt,
  });

  factory ScanHistory.fromJson(Map<String, dynamic> json) {
    return ScanHistory(
      id: json['id'] ?? 0,
      passId: (json['passId'] ?? 'N/A').toString(),
      qrCode: (json['qrCode'] ?? '').toString(),
      bookingNumber: (json['bookingNumber'] ?? 'N/A').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(), // ✅ ADDED
      checkedInDatetime: _parseDate(json['checkedInDatetime']),
      activatedAt: _parseDate(json['activatedAt']),
      qrExpiresAt: _parseDate(json['qrExpiresAt']),
      validUpto: _parseDate(json['validUpto']),
      createdAt: _parseDate(json['createdAt']),
    );
  }

  /// 🔥 Safe Date Parser - Handles multiple formats
  static DateTime? _parseDate(dynamic date) {
    if (date == null) return null;
    try {
      final dateStr = date.toString().trim();

      // Try ISO 8601 format first (2026-04-20T16:21:00)
      try {
        return DateTime.parse(dateStr);
      } catch (e) {
        // If that fails, try DD-MM-YYYY HH:MM format (20-04-2026 16:21)
        final dateFormat = DateFormat('dd-MM-yyyy HH:mm');
        return dateFormat.parse(dateStr);
      }
    } catch (e) {
      print('❌ Failed to parse date: $date, Error: $e');
      return null;
    }
  }

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

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

  /// ✅ UI Friendly Getters

  String get displayCheckedIn => _formatDate(checkedInDatetime);

  String get displayActivatedAt => _formatDate(activatedAt);

  String get displayQrExpiresAt => _formatDate(qrExpiresAt);

  String get displayValidUpto => _formatDate(validUpto);

  String get displayCreatedAt => _formatDate(createdAt);

  String get displayStaffName =>
      scannedByPartnerStaffName.isNotEmpty
          ? scannedByPartnerStaffName
          : 'N/A';

  String get displayStatus =>
      status.isNotEmpty ? status : 'N/A';

  String get displayReason =>
      reason.isNotEmpty ? reason : 'N/A';

  String get displayCustomerName =>
      customerName.isNotEmpty ? customerName : 'N/A';

  String get displayEmail =>
      customerEmail.isNotEmpty ? customerEmail : 'N/A';

  String get displayAttraction =>
      attractionTitle.isNotEmpty ? attractionTitle : 'N/A';

  /// 🎯 STATUS HELPERS (VERY USEFUL FOR UI)

  bool get isSuccess => status.toLowerCase() == 'success';

  bool get isFailed => status.toLowerCase() == 'failed';

  /// 🎨 Optional: Status Color Helper (UI usage)
  String get statusColorHex {
    if (isSuccess) return '#4CAF50'; // green
    if (isFailed) return '#F44336'; // red
    return '#9E9E9E'; // gre
  }
}