package com.ttstd.signaling.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Table;

import java.io.Serializable;
import java.time.Instant;

/**
 * 被控端黑名单：被控端拉黑的主控端账号。
 *
 * <p>当被拉黑的主控端向该设备发起 OFFER 时，服务端在转发前直接拒绝，
 * 即便双方存在绑定关系也不予放行（黑名单优先级高于绑定）。
 */
@Entity
@Table(name = "device_blacklist")
@IdClass(DeviceBlacklistEntry.PK.class)
public class DeviceBlacklistEntry {

    @Id
    @Column(name = "device_uid", length = 48, nullable = false)
    private String deviceUid;

    @Id
    @Column(name = "blocked_user_id", length = 48, nullable = false)
    private String blockedUserId;

    @Column(name = "reason", length = 255)
    private String reason;

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

    protected DeviceBlacklistEntry() {
    }

    public DeviceBlacklistEntry(String deviceUid, String blockedUserId, String reason) {
        this.deviceUid = deviceUid;
        this.blockedUserId = blockedUserId;
        this.reason = reason;
        this.createdAt = Instant.now();
    }

    public String getDeviceUid() { return deviceUid; }
    public String getBlockedUserId() { return blockedUserId; }
    public String getReason() { return reason; }
    public void setReason(String reason) { this.reason = reason; }
    public Instant getCreatedAt() { return createdAt; }

    /** 复合主键 */
    public static class PK implements Serializable {
        private String deviceUid;
        private String blockedUserId;

        public PK() {
        }

        public PK(String deviceUid, String blockedUserId) {
            this.deviceUid = deviceUid;
            this.blockedUserId = blockedUserId;
        }

        public String getDeviceUid() { return deviceUid; }
        public void setDeviceUid(String deviceUid) { this.deviceUid = deviceUid; }
        public String getBlockedUserId() { return blockedUserId; }
        public void setBlockedUserId(String blockedUserId) { this.blockedUserId = blockedUserId; }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof PK pk)) {
                return false;
            }
            return java.util.Objects.equals(deviceUid, pk.deviceUid)
                    && java.util.Objects.equals(blockedUserId, pk.blockedUserId);
        }

        @Override
        public int hashCode() {
            return java.util.Objects.hash(deviceUid, blockedUserId);
        }
    }
}
