package com.gcsc.connection.service.entity;

import com.gcsc.connection.common.entity.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "snp_service_api_spec", schema = "common")
public class SnpServiceApiSpec extends BaseEntity {

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

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "api_id", nullable = false, unique = true)
    private SnpServiceApi api;

    @Column(name = "sample_url", length = 1000)
    private String sampleUrl;

    @Column(name = "sample_code", columnDefinition = "TEXT")
    private String sampleCode;

    @Column(name = "request_body_example", columnDefinition = "TEXT")
    private String requestBodyExample;

    @Column(name = "response_body_example", columnDefinition = "TEXT")
    private String responseBodyExample;

    @Column(name = "auth_required", nullable = false)
    private Boolean authRequired = false;

    @Column(name = "auth_type", length = 20)
    private String authType;

    @Column(name = "deprecated", nullable = false)
    private Boolean deprecated = false;

    @Column(name = "data_format", length = 100)
    private String dataFormat;

    @Column(name = "reference_url", length = 500)
    private String referenceUrl;

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

    @Builder
    public SnpServiceApiSpec(SnpServiceApi api, String sampleUrl, String sampleCode,
                              String requestBodyExample, String responseBodyExample,
                              Boolean authRequired, String authType, Boolean deprecated,
                              String dataFormat, String referenceUrl, String note) {
        this.api = api;
        this.sampleUrl = sampleUrl;
        this.sampleCode = sampleCode;
        this.requestBodyExample = requestBodyExample;
        this.responseBodyExample = responseBodyExample;
        this.authRequired = authRequired != null ? authRequired : false;
        this.authType = authType;
        this.deprecated = deprecated != null ? deprecated : false;
        this.dataFormat = dataFormat;
        this.referenceUrl = referenceUrl;
        this.note = note;
    }

    public void update(String sampleUrl, String sampleCode, String requestBodyExample,
                       String responseBodyExample, Boolean authRequired, String authType,
                       Boolean deprecated, String dataFormat, String referenceUrl, String note) {
        if (sampleUrl != null) this.sampleUrl = sampleUrl;
        if (sampleCode != null) this.sampleCode = sampleCode;
        if (requestBodyExample != null) this.requestBodyExample = requestBodyExample;
        if (responseBodyExample != null) this.responseBodyExample = responseBodyExample;
        if (authRequired != null) this.authRequired = authRequired;
        if (authType != null) this.authType = authType;
        if (deprecated != null) this.deprecated = deprecated;
        if (dataFormat != null) this.dataFormat = dataFormat;
        if (referenceUrl != null) this.referenceUrl = referenceUrl;
        if (note != null) this.note = note;
    }
}
