package com.gcsc.connection.monitoring.entity;

import com.gcsc.connection.apikey.entity.SnpApiKey;
import com.gcsc.connection.service.entity.SnpService;
import com.gcsc.connection.tenant.entity.SnpTenant;
import com.gcsc.connection.user.entity.SnpUser;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@Entity
@Table(name = "snp_api_request_log", schema = "common")
public class SnpApiRequestLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "log_id")
    private Long logId;

    @Column(name = "request_url", length = 2000)
    private String requestUrl;

    @Column(name = "request_params", columnDefinition = "TEXT")
    private String requestParams;

    @Column(name = "request_method", length = 10)
    private String requestMethod;

    @Column(name = "request_status", length = 20)
    private String requestStatus;

    @Column(name = "request_headers", columnDefinition = "TEXT")
    private String requestHeaders;

    @Column(name = "request_ip", length = 45)
    private String requestIp;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private SnpService service;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private SnpUser user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "api_key_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private SnpApiKey apiKey;

    @Column(name = "response_size")
    private Long responseSize;

    @Column(name = "response_time")
    private Integer responseTime;

    @Column(name = "response_status")
    private Integer responseStatus;

    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    @Column(name = "requested_at", nullable = false)
    private LocalDateTime requestedAt;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tenant_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private SnpTenant tenant;

    @Builder
    public SnpApiRequestLog(String requestUrl, String requestParams, String requestMethod,
                            String requestStatus, String requestHeaders, String requestIp,
                            SnpService service, SnpUser user, SnpApiKey apiKey,
                            Long responseSize, Integer responseTime, Integer responseStatus,
                            String errorMessage, LocalDateTime requestedAt, SnpTenant tenant) {
        this.requestUrl = requestUrl;
        this.requestParams = requestParams;
        this.requestMethod = requestMethod;
        this.requestStatus = requestStatus;
        this.requestHeaders = requestHeaders;
        this.requestIp = requestIp;
        this.service = service;
        this.user = user;
        this.apiKey = apiKey;
        this.responseSize = responseSize;
        this.responseTime = responseTime;
        this.responseStatus = responseStatus;
        this.errorMessage = errorMessage;
        this.requestedAt = requestedAt != null ? requestedAt : LocalDateTime.now();
        this.tenant = tenant;
    }
}
