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.ManyToOne;
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_param", schema = "common")
public class SnpServiceApiParam extends BaseEntity {

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

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

    @Column(name = "param_type", length = 10, nullable = false)
    private String paramType;

    @Column(name = "param_name", length = 100, nullable = false)
    private String paramName;

    @Column(name = "param_meaning", length = 200)
    private String paramMeaning;

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

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

    @Column(name = "default_value", length = 200)
    private String defaultValue;

    @Column(name = "input_type", length = 20)
    private String inputType;

    @Column(name = "sort_order", nullable = false)
    private Integer sortOrder = 0;

    @Builder
    public SnpServiceApiParam(SnpServiceApi api, String paramType, String paramName,
                               String paramMeaning, String paramDescription, Boolean required,
                               String defaultValue, String inputType, Integer sortOrder) {
        this.api = api;
        this.paramType = paramType;
        this.paramName = paramName;
        this.paramMeaning = paramMeaning;
        this.paramDescription = paramDescription;
        this.required = required != null ? required : false;
        this.defaultValue = defaultValue;
        this.inputType = inputType;
        this.sortOrder = sortOrder != null ? sortOrder : 0;
    }

    public void update(String paramType, String paramName, String paramMeaning,
                       String paramDescription, Boolean required, String defaultValue,
                       String inputType, Integer sortOrder) {
        if (paramType != null) this.paramType = paramType;
        if (paramName != null) this.paramName = paramName;
        if (paramMeaning != null) this.paramMeaning = paramMeaning;
        if (paramDescription != null) this.paramDescription = paramDescription;
        if (required != null) this.required = required;
        if (defaultValue != null) this.defaultValue = defaultValue;
        if (inputType != null) this.inputType = inputType;
        if (sortOrder != null) this.sortOrder = sortOrder;
    }
}
