package com.honey.honey.model;

import jakarta.persistence.*;
import lombok.*;

import java.time.Instant;

@Entity
@Table(name = "transactions")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Transaction {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "user_id", nullable = false)
    private Integer userId;

    @Column(name = "amount", nullable = false)
    private Long amount; // Amount in bigint format (positive for credits, negative for debits)

    @Enumerated(EnumType.STRING)
    @Column(name = "type", nullable = false, length = 50, columnDefinition = "VARCHAR(50)")
    private TransactionType type;

    @Column(name = "task_id")
    private Integer taskId; // Task ID for TASK_BONUS type

    @Column(name = "created_at", nullable = false, updatable = false)
    private Instant createdAt;

    @PrePersist
    protected void onCreate() {
        // Only set createdAt if it's not already set (allows custom timestamps)
        if (createdAt == null) {
            createdAt = Instant.now();
        }
    }

    public enum TransactionType {
        DEPOSIT,      // Payment/deposit
        WITHDRAWAL,   // Payout/withdrawal
        TASK_BONUS,   // Task reward
        CANCELLATION_OF_WITHDRAWAL  // Cancellation of withdrawal (payout cancelled by admin)
    }
}

