package com.mzh.library.entity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class SystemLog {
    private static final DateTimeFormatter DISPLAY_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    private long id;
    private Long operatorId;
    private String operatorUsername;
    private String operatorDisplayName;
    private String operatorRole;
    private String operationType;
    private String targetTable;
    private String targetId;
    private String resultStatus;
    private String message;
    private String requestIp;
    private LocalDateTime createdAt;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Long getOperatorId() {
        return operatorId;
    }

    public void setOperatorId(Long operatorId) {
        this.operatorId = operatorId;
    }

    public String getOperatorUsername() {
        return operatorUsername;
    }

    public void setOperatorUsername(String operatorUsername) {
        this.operatorUsername = operatorUsername;
    }

    public String getOperatorDisplayName() {
        return operatorDisplayName;
    }

    public void setOperatorDisplayName(String operatorDisplayName) {
        this.operatorDisplayName = operatorDisplayName;
    }

    public String getOperatorRole() {
        return operatorRole;
    }

    public void setOperatorRole(String operatorRole) {
        this.operatorRole = operatorRole;
    }

    public String getOperationType() {
        return operationType;
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    public String getTargetTable() {
        return targetTable;
    }

    public void setTargetTable(String targetTable) {
        this.targetTable = targetTable;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public String getResultStatus() {
        return resultStatus;
    }

    public void setResultStatus(String resultStatus) {
        this.resultStatus = resultStatus;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getRequestIp() {
        return requestIp;
    }

    public void setRequestIp(String requestIp) {
        this.requestIp = requestIp;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public String getCreatedAtText() {
        return createdAt == null ? "" : DISPLAY_FORMAT.format(createdAt);
    }

    public String getOperatorLabel() {
        String displayName = trim(operatorDisplayName);
        if (!displayName.isEmpty()) {
            return displayName;
        }

        String username = trim(operatorUsername);
        if (!username.isEmpty()) {
            return username;
        }

        return operatorId == null ? "系统" : "用户 #" + operatorId;
    }

    public String getOperatorMetaText() {
        StringBuilder meta = new StringBuilder();
        String displayName = trim(operatorDisplayName);
        String username = trim(operatorUsername);
        if (!username.isEmpty() && !username.equals(displayName)) {
            appendMeta(meta, username);
        }
        if (operatorId != null && (!displayName.isEmpty() || !username.isEmpty())) {
            appendMeta(meta, "#" + operatorId);
        }
        appendMeta(meta, displayRole(operatorRole));
        return meta.toString();
    }

    public String getResultStatusCode() {
        String normalized = trim(resultStatus).toLowerCase(Locale.ROOT);
        if ("success".equals(normalized) || "failure".equals(normalized)) {
            return normalized;
        }
        return "unknown";
    }

    public String getResultStatusName() {
        String normalized = trim(resultStatus).toLowerCase(Locale.ROOT);
        if ("success".equals(normalized)) {
            return "成功";
        }
        if ("failure".equals(normalized)) {
            return "失败";
        }
        return normalized.isEmpty() ? "未知" : trim(resultStatus);
    }

    public String getTargetTableName() {
        String normalized = trim(targetTable).toLowerCase(Locale.ROOT);
        if ("users".equals(normalized)) {
            return "用户";
        }
        return trim(targetTable);
    }

    private void appendMeta(StringBuilder meta, String value) {
        if (value.isEmpty()) {
            return;
        }
        if (meta.length() > 0) {
            meta.append(" ");
        }
        meta.append(value);
    }

    private String trim(String value) {
        return value == null ? "" : value.trim();
    }

    private String displayRole(String roleCode) {
        String normalized = trim(roleCode);
        if (normalized.isEmpty()) {
            return "";
        }
        try {
            return Role.fromCode(normalized).getDisplayName();
        } catch (IllegalArgumentException ex) {
            return normalized;
        }
    }
}
