package com.yaoai.api.dto.settings;

import com.yaoai.domain.entity.TenantModelProvider;
import lombok.Data;

import java.util.List;

@Data
public class ModelProviderDTO {

    private Long id;
    private String name;
    private String protocol;
    private String capability;
    private String baseUrl;
    private String apiKey;       // masked on read: "sk-••••••••xxxx"
    private String secretKey;    // masked on read
    private List<ModelItem> models;

    @Data
    public static class ModelItem {
        private String id;
        private String name;
        private boolean checked;
    }

    public static ModelProviderDTO from(TenantModelProvider p) {
        ModelProviderDTO dto = new ModelProviderDTO();
        dto.setId(p.getId());
        dto.setName(p.getName());
        dto.setProtocol(p.getProtocol());
        dto.setCapability(p.getCapability());
        dto.setBaseUrl(p.getBaseUrl());
        dto.setApiKey(maskKey(p.getApiKey()));
        dto.setSecretKey(maskKey(p.getSecretKey()));
        // modelsJson → List parsed by controller
        return dto;
    }

    private static String maskKey(String key) {
        if (key == null || key.isBlank()) return "";
        int len = key.length();
        if (len <= 8) return "••••••••";
        return "sk-••••••••" + key.substring(len - 4);
    }
}
