package cn.novalon.gym.manage.checkIn.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.time.LocalDateTime;

/**
 * 会员到店签到记录实体
 *
 * @author 付嘉
 * @date 2026-06-08
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("sign_in_record")
public class SignInRecord {

    /**
     * 自增主键
     */
    @Id
    private Long id;

    /**
     * 会员ID，关联member表
     */
    @Column("member_id")
    private Long memberId;

    /**
     * 签到时使用的会员卡ID
     */
    @Column("member_card_id")
    private Long memberCardId;

    /**
     * 签到入场时间
     */
    @Column("sign_in_time")
    private LocalDateTime signInTime;

    /**
     * 签到方式：QR_CODE-扫码签到，MANUAL-手动签到，FACE-人脸识别
     */
    @Column("sign_in_type")
    private String signInType;

    /**
     * 签到状态：SUCCESS-成功，FAILED-失败
     */
    @Column("sign_in_status")
    private String signInStatus;

    /**
     * JSONB格式，存储会员卡验证时的快照数据
     */
    @Column("verification_details")
    private String verificationDetails;

    /**
     * 失败时的具体原因文案
     */
    @Column("fail_reason")
    private String failReason;

    /**
     * 操作人ID（前台人员），自助签到时为NULL
     */
    @Column("operator_id")
    private Long operatorId;

    /**
     * 操作人姓名冗余
     */
    @Column("operator_name")
    private String operatorName;

    /**
     * 签到设备标识或型号
     */
    @Column("device_info")
    private String deviceInfo;

    /**
     * 客户端IP地址
     */
    @Column("ip_address")
    private String ipAddress;

    /**
     * 签到来源：MINI_PROGRAM-小程序扫码，PC_BACKEND-后台管理端
     */
    @Column("source")
    private String source;

    /**
     * 软删除标识：false-未删除，true-已删除
     */
    @Column("is_delete")
    private Boolean isDelete;

    /**
     * 记录创建时间
     */
    @CreatedDate
    @Column("created_at")
    private LocalDateTime createdAt;

    /**
     * 记录更新时间
     */
    @LastModifiedDate
    @Column("updated_at")
    private LocalDateTime updatedAt;

    // ========== 常量定义 ==========

    /**
     * 签到类型常量
     */
    public static final class SignInType {
        /** 扫码签到 */
        public static final String QR_CODE = "QR_CODE";
        /** 手动签到 */
        public static final String MANUAL = "MANUAL";
        /** 人脸识别 */
        public static final String FACE = "FACE";

        private SignInType() {}
    }

    /**
     * 签到状态常量
     */
    public static final class SignInStatus {
        /** 成功 */
        public static final String SUCCESS = "SUCCESS";
        /** 失败 */
        public static final String FAILED = "FAILED";

        private SignInStatus() {}
    }

    /**
     * 签到来源常量
     */
    public static final class Source {
        /** 小程序扫码 */
        public static final String MINI_PROGRAM = "MINI_PROGRAM";
        /** 后台管理端手动签到 */
        public static final String PC_BACKEND = "PC_BACKEND";

        private Source() {}
    }

    // ========== 辅助方法 ==========

    /**
     * 判断签到是否成功
     */
    public boolean isSuccess() {
        return SignInStatus.SUCCESS.equals(this.signInStatus);
    }

    /**
     * 判断签到是否失败
     */
    public boolean isFailed() {
        return SignInStatus.FAILED.equals(this.signInStatus);
    }

    /**
     * 判断是否为扫码签到
     */
    public boolean isQrCodeSign() {
        return SignInType.QR_CODE.equals(this.signInType);
    }

    /**
     * 判断是否已删除
     */
    public boolean isDeleted() {
        return Boolean.TRUE.equals(this.isDelete);
    }

    /**
     * 软删除
     */
    public void softDelete() {
        this.isDelete = true;
    }

    /**
     * 恢复删除
     */
    public void restore() {
        this.isDelete = false;
    }
}
