package com.gameplatform.server.model.entity.log;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.gameplatform.server.model.enums.ScriptOperationType;

import java.time.LocalDateTime;

/**
 * 脚本操作记录实体
 */
@TableName("script_operation_log")
public class ScriptOperationLog {
    
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    
    @TableField("operation_type")
    private ScriptOperationType operationType;
    
    @TableField("machine_id")
    private String machineId;
    
    @TableField("request_url")
    private String requestUrl;
    
    @TableField("request_params")
    private String requestParams; // JSON字符串
    
    @TableField("response_result")
    private String responseResult;
    
    @TableField("success")
    private Boolean success;
    
    @TableField("error_message")
    private String errorMessage;
    
    @TableField("duration_ms")
    private Integer durationMs;
    
    @TableField("request_time")
    private LocalDateTime requestTime;
    
    @TableField("response_time")
    private LocalDateTime responseTime;
    
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    @TableField("updated_at")
    private LocalDateTime updatedAt;

    // 构造函数
    public ScriptOperationLog() {}

    public ScriptOperationLog(ScriptOperationType operationType, String machineId, String requestUrl) {
        this.operationType = operationType;
        this.machineId = machineId;
        this.requestUrl = requestUrl;
        this.requestTime = LocalDateTime.now();
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
        this.success = false; // 默认失败，成功时再更新
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

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

    public ScriptOperationType getOperationType() {
        return operationType;
    }

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

    public String getMachineId() {
        return machineId;
    }

    public void setMachineId(String machineId) {
        this.machineId = machineId;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public String getRequestParams() {
        return requestParams;
    }

    public void setRequestParams(String requestParams) {
        this.requestParams = requestParams;
    }

    public String getResponseResult() {
        return responseResult;
    }

    public void setResponseResult(String responseResult) {
        this.responseResult = responseResult;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Integer getDurationMs() {
        return durationMs;
    }

    public void setDurationMs(Integer durationMs) {
        this.durationMs = durationMs;
    }

    public LocalDateTime getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(LocalDateTime requestTime) {
        this.requestTime = requestTime;
    }

    public LocalDateTime getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(LocalDateTime responseTime) {
        this.responseTime = responseTime;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

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

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    // 便捷方法：设置成功响应
    public void setSuccessResponse(String result, LocalDateTime responseTime, long durationMs) {
        this.responseResult = result;
        this.success = true;
        this.responseTime = responseTime;
        this.durationMs = (int) durationMs;
        this.updatedAt = LocalDateTime.now();
    }

    // 便捷方法：设置失败响应
    public void setFailureResponse(String errorMessage, LocalDateTime responseTime, long durationMs) {
        this.errorMessage = errorMessage;
        this.success = false;
        this.responseTime = responseTime;
        this.durationMs = (int) durationMs;
        this.updatedAt = LocalDateTime.now();
    }

    @Override
    public String toString() {
        return "ScriptOperationLog{" +
                "id=" + id +
                ", operationType=" + operationType +
                ", machineId='" + machineId + '\'' +
                ", requestUrl='" + requestUrl + '\'' +
                ", success=" + success +
                ", durationMs=" + durationMs +
                ", requestTime=" + requestTime +
                ", responseTime=" + responseTime +
                '}';
    }
}
