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 'quote_model.g.dart';

@HiveType(typeId: HiveTypeIds.quoteModel)
class QuoteModel extends HiveObject {
  QuoteModel({
    required this.quoteId,
    required this.quoteNumber,
    required this.userId,
    required this.status,
    required this.totalAmount,
    required this.discountAmount,
    required this.finalAmount,
    this.projectName,
    this.deliveryAddress,
    this.paymentTerms,
    this.notes,
    this.validUntil,
    this.convertedOrderId,
    this.erpnextQuotation,
    required this.createdAt,
    this.updatedAt,
  });

  @HiveField(0)
  final String quoteId;
  @HiveField(1)
  final String quoteNumber;
  @HiveField(2)
  final String userId;
  @HiveField(3)
  final QuoteStatus status;
  @HiveField(4)
  final double totalAmount;
  @HiveField(5)
  final double discountAmount;
  @HiveField(6)
  final double finalAmount;
  @HiveField(7)
  final String? projectName;
  @HiveField(8)
  final String? deliveryAddress;
  @HiveField(9)
  final String? paymentTerms;
  @HiveField(10)
  final String? notes;
  @HiveField(11)
  final DateTime? validUntil;
  @HiveField(12)
  final String? convertedOrderId;
  @HiveField(13)
  final String? erpnextQuotation;
  @HiveField(14)
  final DateTime createdAt;
  @HiveField(15)
  final DateTime? updatedAt;

  factory QuoteModel.fromJson(Map<String, dynamic> json) => QuoteModel(
    quoteId: json['quote_id'] as String,
    quoteNumber: json['quote_number'] as String,
    userId: json['user_id'] as String,
    status: QuoteStatus.values.firstWhere((e) => e.name == json['status']),
    totalAmount: (json['total_amount'] as num).toDouble(),
    discountAmount: (json['discount_amount'] as num).toDouble(),
    finalAmount: (json['final_amount'] as num).toDouble(),
    projectName: json['project_name'] as String?,
    deliveryAddress: json['delivery_address'] != null
        ? jsonEncode(json['delivery_address'])
        : null,
    paymentTerms: json['payment_terms'] as String?,
    notes: json['notes'] as String?,
    validUntil: json['valid_until'] != null
        ? DateTime.parse(json['valid_until']?.toString() ?? '')
        : null,
    convertedOrderId: json['converted_order_id'] as String?,
    erpnextQuotation: json['erpnext_quotation'] as String?,
    createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
    updatedAt: json['updated_at'] != null
        ? DateTime.parse(json['updated_at']?.toString() ?? '')
        : null,
  );

  Map<String, dynamic> toJson() => {
    'quote_id': quoteId,
    'quote_number': quoteNumber,
    'user_id': userId,
    'status': status.name,
    'total_amount': totalAmount,
    'discount_amount': discountAmount,
    'final_amount': finalAmount,
    'project_name': projectName,
    'delivery_address': deliveryAddress != null
        ? jsonDecode(deliveryAddress!)
        : null,
    'payment_terms': paymentTerms,
    'notes': notes,
    'valid_until': validUntil?.toIso8601String(),
    'converted_order_id': convertedOrderId,
    'erpnext_quotation': erpnextQuotation,
    'created_at': createdAt.toIso8601String(),
    'updated_at': updatedAt?.toIso8601String(),
  };

  bool get isExpired =>
      validUntil != null && DateTime.now().isAfter(validUntil!);
  bool get isConverted => convertedOrderId != null;
}
