import 'package:hive/hive.dart';

part 'transaction_model.g.dart';

@HiveType(typeId: 3)
enum TransactionType {
  @HiveField(0)
  revenue, // Receita
  @HiveField(1)
  expense, // Despesa
}

@HiveType(typeId: 4)
class TransactionModel extends HiveObject {
  @HiveField(0)
  final String id;
  
  @HiveField(1)
  final String userId;
  
  @HiveField(2)
  final TransactionType type;
  
  @HiveField(3)
  final double amount;
  
  @HiveField(4)
  final String description;
  
  @HiveField(5)
  final DateTime dueDate;
  
  @HiveField(6)
  final DateTime? paidDate;
  
  @HiveField(7)
  final bool isPaid;
  
  @HiveField(8)
  final String? haircutId; // Vinculado a um corte (receita automática)
  
  @HiveField(9)
  final DateTime createdAt;

  TransactionModel({
    required this.id,
    required this.userId,
    required this.type,
    required this.amount,
    required this.description,
    required this.dueDate,
    this.paidDate,
    required this.isPaid,
    this.haircutId,
    required this.createdAt,
  });

  bool get isFromHaircut => haircutId != null;
  
  bool get isOverdue => !isPaid && dueDate.isBefore(DateTime.now());

  TransactionModel copyWith({
    String? id,
    String? userId,
    TransactionType? type,
    double? amount,
    String? description,
    DateTime? dueDate,
    DateTime? paidDate,
    bool? isPaid,
    String? haircutId,
    DateTime? createdAt,
  }) {
    return TransactionModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      type: type ?? this.type,
      amount: amount ?? this.amount,
      description: description ?? this.description,
      dueDate: dueDate ?? this.dueDate,
      paidDate: paidDate ?? this.paidDate,
      isPaid: isPaid ?? this.isPaid,
      haircutId: haircutId ?? this.haircutId,
      createdAt: createdAt ?? this.createdAt,
    );
  }
}
