package com.bizzan.bitrade.entity;

import com.bizzan.bitrade.constant.CoinRechargeOrderStatus;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

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

/**
 * 链上充值订单表
 * 记录用户发起充值到链上确认到账的完整流程：
 * 创建订单（PENDING）→ 用户提交 hash（CONFIRMING）→ 链上确认（COMPLETED/FAILED）
 *
 * @author auto
 * @date 2026-05-13
 */
@Entity
@Data
@Table(name = "coin_recharge_order",
        indexes = {
                @Index(name = "idx_member_id",   columnList = "member_id"),
                @Index(name = "idx_order_no",    columnList = "order_no",  unique = true),
                @Index(name = "idx_tx_hash",     columnList = "tx_hash"),
                @Index(name = "idx_status",      columnList = "status")
        })
public class CoinRechargeOrder {

    /** 主键，自增 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 订单号，全局唯一，格式：RC + yyyyMMddHHmmss + 6位随机数
     */
    @ApiModelProperty("订单号")
    @Column(name = "order_no", nullable = false, unique = true, length = 32,
            columnDefinition = "varchar(32) not null comment '订单号，全局唯一'")
    private String orderNo;

    /**
     * 会员ID，关联 member.id
     */
    @ApiModelProperty("会员ID")
    @Column(name = "member_id", nullable = false,
            columnDefinition = "bigint not null comment '会员ID，关联 member.id'")
    private Long memberId;

    /**
     * 子币种名称，关联 coin.name，如 EUSDT / TUSDT
     * 子币种决定了使用哪条主网充值
     */
    @ApiModelProperty("子币种名称，如 EUSDT")
    @Column(name = "coin_name", nullable = false, length = 20,
            columnDefinition = "varchar(20) not null comment '子币种名称，关联 coin.name'")
    private String coinName;

    /**
     * 主网配置ID，关联 coin_network_config.id
     * 记录本次充值使用的主网及收款地址快照来源
     */
    @ApiModelProperty("主网配置ID，关联 coin_network_config.id")
    @Column(name = "network_config_id", nullable = false,
            columnDefinition = "bigint not null comment '主网配置ID，关联 coin_network_config.id'")
    private Long networkConfigId;

    /**
     * 主网名称快照，如 ERC20 / TRC20，冗余存储防止主网信息变更影响历史记录
     */
    @ApiModelProperty("主网名称快照，如 ERC20")
    @Column(name = "network_name", nullable = false, length = 50,
            columnDefinition = "varchar(50) not null comment '主网名称快照'")
    private String networkName;

    /**
     * 收款地址快照，来自 coin_network_config.recharge_address
     * 冗余存储，防止收款地址变更影响历史记录
     */
    @ApiModelProperty("收款地址快照")
    @Column(name = "recharge_address", nullable = false, length = 200,
            columnDefinition = "varchar(200) not null comment '收款地址快照'")
    private String rechargeAddress;

    /**
     * 用户申报的充值金额
     */
    @ApiModelProperty("充值金额")
    @Column(name = "amount", nullable = false,
            columnDefinition = "decimal(18,8) not null comment '充值金额'")
    private BigDecimal amount;

    /**
     * 链上交易 hash
     * 用户通过第三方钱包完成转账后回写，状态同步变为 CONFIRMING
     */
    @ApiModelProperty("链上交易 hash，用户回写")
    @Column(name = "tx_hash", length = 128,
            columnDefinition = "varchar(128) default null comment '链上交易 hash'")
    private String txHash;

    /**
     * 订单状态
     *
     * @see CoinRechargeOrderStatus
     */
    @ApiModelProperty("订单状态：0=待支付 1=待链上确认 2=已完成 3=已取消 4=失败")
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", nullable = false,
            columnDefinition = "int not null default 0 comment '订单状态 0待支付 1待链上确认 2已完成 3已取消 4失败'")
    private CoinRechargeOrderStatus status = CoinRechargeOrderStatus.PENDING;

    /**
     * 备注，可由用户填写或系统写入失败原因
     */
    @ApiModelProperty("备注")
    @Column(name = "remark", length = 255,
            columnDefinition = "varchar(255) default null comment '备注'")
    private String remark;

    /**
     * 创建时间
     */
    @ApiModelProperty("创建时间")
    @CreationTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "create_time", updatable = false,
            columnDefinition = "datetime not null comment '创建时间'")
    private Date createTime;

    /**
     * hash 回写时间（用户提交 hash 的时间）
     */
    @ApiModelProperty("hash 回写时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "submit_hash_time",
            columnDefinition = "datetime default null comment 'hash 回写时间'")
    private Date submitHashTime;

    /**
     * 最后更新时间
     */
    @ApiModelProperty("最后更新时间")
    @UpdateTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "update_time",
            columnDefinition = "datetime not null comment '最后更新时间'")
    private Date updateTime;
}
