import 'dart:convert';
import 'package:hive_ce/hive.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/core/database/models/enums.dart';

part 'points_record_model.g.dart';

@HiveType(typeId: HiveTypeIds.pointsRecordModel)
class PointsRecordModel extends HiveObject {
  PointsRecordModel({
    required this.recordId,
    required this.userId,
    required this.invoiceNumber,
    required this.storeName,
    required this.transactionDate,
    required this.invoiceAmount,
    this.notes,
    this.attachments,
    required this.status,
    this.rejectReason,
    this.pointsEarned,
    required this.submittedAt,
    this.processedAt,
    this.processedBy,
  });

  @HiveField(0)
  final String recordId;
  @HiveField(1)
  final String userId;
  @HiveField(2)
  final String invoiceNumber;
  @HiveField(3)
  final String storeName;
  @HiveField(4)
  final DateTime transactionDate;
  @HiveField(5)
  final double invoiceAmount;
  @HiveField(6)
  final String? notes;
  @HiveField(7)
  final String? attachments;
  @HiveField(8)
  final PointsStatus status;
  @HiveField(9)
  final String? rejectReason;
  @HiveField(10)
  final int? pointsEarned;
  @HiveField(11)
  final DateTime submittedAt;
  @HiveField(12)
  final DateTime? processedAt;
  @HiveField(13)
  final String? processedBy;

  factory PointsRecordModel.fromJson(Map<String, dynamic> json) =>
      PointsRecordModel(
        recordId: json['record_id'] as String,
        userId: json['user_id'] as String,
        invoiceNumber: json['invoice_number'] as String,
        storeName: json['store_name'] as String,
        transactionDate: DateTime.parse(
          json['transaction_date']?.toString() ?? '',
        ),
        invoiceAmount: (json['invoice_amount'] as num).toDouble(),
        notes: json['notes'] as String?,
        attachments: json['attachments'] != null
            ? jsonEncode(json['attachments'])
            : null,
        status: PointsStatus.values.firstWhere((e) => e.name == json['status']),
        rejectReason: json['reject_reason'] as String?,
        pointsEarned: json['points_earned'] as int?,
        submittedAt: DateTime.parse(json['submitted_at']?.toString() ?? ''),
        processedAt: json['processed_at'] != null
            ? DateTime.parse(json['processed_at']?.toString() ?? '')
            : null,
        processedBy: json['processed_by'] as String?,
      );

  Map<String, dynamic> toJson() => {
    'record_id': recordId,
    'user_id': userId,
    'invoice_number': invoiceNumber,
    'store_name': storeName,
    'transaction_date': transactionDate.toIso8601String(),
    'invoice_amount': invoiceAmount,
    'notes': notes,
    'attachments': attachments != null ? jsonDecode(attachments!) : null,
    'status': status.name,
    'reject_reason': rejectReason,
    'points_earned': pointsEarned,
    'submitted_at': submittedAt.toIso8601String(),
    'processed_at': processedAt?.toIso8601String(),
    'processed_by': processedBy,
  };

  List<String>? get attachmentsList {
    if (attachments == null) return null;
    try {
      final decoded = jsonDecode(attachments!) as List;
      return decoded.map((e) => e.toString()).toList();
    } catch (e) {
      return null;
    }
  }
}
