package com.bizzan.bitrade.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 用户体验金券记录表
 */
@Entity
@Data
@Table(name = "experience_gift_record")
public class ExperienceGiftRecord {

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

    /** 来源产品ID */
    @Column(name = "product_id", nullable = false)
    private Long productId;

    /** 持有用户ID */
    @Column(name = "member_id", nullable = false)
    private Long memberId;

    /** 产品名称快照 */
    @Column(name = "product_name", nullable = false)
    private String productName;

    /** 体验金面额快照（USDT） */
    @Column(name = "amount", nullable = false)
    private BigDecimal amount;

    /** 盈利上限快照（USDT） */
    @Column(name = "max_profit", nullable = false)
    private BigDecimal maxProfit;

    /** 杠杆倍数快照 */
    @Column(name = "leverage", nullable = false)
    private Integer leverage;

    /** 有效期天数快照 */
    @Column(name = "validity_days", nullable = false)
    private Integer validityDays;

    /** 发放来源 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "grant_type", nullable = false)
    private ExperienceGiftGrantType grantType;

    /** 邀请人ID */
    @Column(name = "inviter_id")
    private Long inviterId;

    /**
     * 状态：PENDING=未使用，IN_USE=持仓中，USED=已使用，EXPIRED=已过期，INVALID=已失效
     * 具体平仓原因见 closeReason 字段
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", nullable = false)
    private ExperienceGiftRecordStatus status = ExperienceGiftRecordStatus.PENDING;

    /** 到期时间 */
    @Column(name = "expire_at", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date expireAt;

    /** 开仓时间 */
    @Column(name = "used_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date usedAt;

    /** 平仓/失效时间 */
    @Column(name = "closed_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date closedAt;

    /** 开仓交易对 */
    @Column(name = "coin_symbol")
    private String coinSymbol;

    /** 仓位方向 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "position_direction")
    private ContractOrderDirection positionDirection;

    /** 关联仓位ID */
    @Column(name = "position_id")
    private Long positionId;

    /** 盈利上限触发价（系统自动设置，基于 maxProfit 计算） */
    @Column(name = "auto_close_price")
    private BigDecimal autoClosePrice;

    /**
     * 用户设置的止盈触发价（0 或 null 表示未设置）
     * 多单：当前价 >= stopProfitPrice 时触发平仓；空单：当前价 <= stopProfitPrice 时触发平仓
     */
    @Column(name = "stop_profit_price")
    private BigDecimal stopProfitPrice;

    /**
     * 用户设置的止损触发价（0 或 null 表示未设置）
     * 多单：当前价 <= stopLossPrice 时触发平仓；空单：当前价 >= stopLossPrice 时触发平仓
     */
    @Column(name = "stop_loss_price")
    private BigDecimal stopLossPrice;

    /** 开仓手续费（USDT） */
    @Column(name = "open_fee")
    private BigDecimal openFee;

    /** 平仓手续费（USDT） */
    @Column(name = "close_fee")
    private BigDecimal closeFee;

    /** 平仓净盈亏（USDT，已扣除开平仓手续费） */
    private BigDecimal pnl;

    /** 平仓原因 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "close_reason")
    private ExperienceGiftCloseReason closeReason;

    /** 设备指纹 */
    @Column(name = "device_fingerprint")
    private String deviceFingerprint;

    /** 领取IP */
    @Column(name = "client_ip")
    private String clientIp;

    /** 手动派发的管理员ID */
    @Column(name = "operator_id")
    private Long operatorId;

    /** 派发备注 */
    @Column(name = "operator_remark")
    private String operatorRemark;

    @Column(name = "created_at", nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdAt;

    @Column(name = "updated_at", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedAt;
}
