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 jakarta.persistence.UniqueConstraint;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@Entity
@Table(name = "snp_service_api", schema = "common",
    uniqueConstraints = @UniqueConstraint(columnNames = {"service_id", "api_path", "api_method"}))
public class SnpServiceApi extends BaseEntity {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_id", nullable = false)
    private SnpService service;

    @Column(name = "api_path", length = 500, nullable = false)
    private String apiPath;

    @Column(name = "api_method", length = 10, nullable = false)
    private String apiMethod;

    @Column(name = "api_name", length = 200, nullable = false)
    private String apiName;

    @Column(name = "api_domain", length = 100)
    private String apiDomain;

    @Column(name = "api_section", length = 100)
    private String apiSection;

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

    @Column(name = "is_active", nullable = false)
    private Boolean isActive = true;

    @Builder
    public SnpServiceApi(SnpService service, String apiPath, String apiMethod, String apiName,
                         String apiDomain, String apiSection, String description, Boolean isActive) {
        this.service = service;
        this.apiPath = apiPath;
        this.apiMethod = apiMethod;
        this.apiName = apiName;
        this.apiDomain = apiDomain;
        this.apiSection = apiSection;
        this.description = description;
        this.isActive = isActive != null ? isActive : true;
    }

    public void update(String apiPath, String apiMethod, String apiName,
                       String apiDomain, String apiSection, String description, Boolean isActive) {
        if (apiPath != null) this.apiPath = apiPath;
        if (apiMethod != null) this.apiMethod = apiMethod;
        if (apiName != null) this.apiName = apiName;
        if (apiDomain != null) this.apiDomain = apiDomain;
        if (apiSection != null) this.apiSection = apiSection;
        if (description != null) this.description = description;
        if (isActive != null) this.isActive = isActive;
    }
}
