import { describe, expect, it } from "vitest";
import {
  buildSettingsUpdatePayload,
  buildSystemSettingsViewModel,
  getDefaultScoreWeights,
  sanitizeSettingsAuditSnapshot,
} from "../router/settings-support.js";

describe("settings support", () => {
  it("builds the admin-facing system settings view model with defaults", () => {
    const runtimeSecrets = {
      azureOpenAiApiKey: {
        configured: true,
        activeSource: "environment" as const,
        hasStoredValue: true,
        envVarNames: ["OPENAI_API_KEY"],
      },
      azureDalleApiKey: {
        configured: false,
        activeSource: "none" as const,
        hasStoredValue: false,
        envVarNames: ["AZURE_DALLE_API_KEY"],
      },
      geminiApiKey: {
        configured: true,
        activeSource: "database" as const,
        hasStoredValue: true,
        envVarNames: ["GEMINI_API_KEY"],
      },
      smtpPassword: {
        configured: false,
        activeSource: "none" as const,
        hasStoredValue: false,
        envVarNames: ["SMTP_PASSWORD"],
      },
      anonymizationSeed: {
        configured: false,
        activeSource: "none" as const,
        hasStoredValue: false,
        envVarNames: ["ANONYMIZATION_SEED"],
      },
    };

    expect(buildSystemSettingsViewModel({
      settings: {
        aiProvider: "azure",
        azureOpenAiEndpoint: "https://example.openai.azure.com",
        scoreVisibleRoles: ["ADMIN"],
        smtpTls: false,
        vacationDefaultDays: 30,
      },
      runtimeSettings: {
        azureOpenAiApiKey: "secret",
        smtpPassword: null,
        azureDalleApiKey: null,
        geminiApiKey: "gemini-secret",
      },
      runtimeSecrets,
      defaultSummaryPrompt: "Summarize this resource.",
    })).toEqual({
      aiProvider: "azure",
      azureOpenAiEndpoint: "https://example.openai.azure.com",
      azureOpenAiDeployment: null,
      azureApiVersion: "2025-01-01-preview",
      aiMaxCompletionTokens: 300,
      aiTemperature: 1,
      aiSummaryPrompt: null,
      defaultSummaryPrompt: "Summarize this resource.",
      hasApiKey: true,
      runtimeSecrets,
      legacyStoredSecretFields: ["azureOpenAiApiKey", "geminiApiKey"],
      scoreWeights: getDefaultScoreWeights(),
      scoreVisibleRoles: ["ADMIN"],
      smtpHost: null,
      smtpPort: 587,
      smtpUser: null,
      smtpFrom: null,
      smtpTls: false,
      hasSmtpPassword: false,
      anonymizationEnabled: false,
      anonymizationDomain: "superhartmut.de",
      anonymizationMode: "global",
      azureDalleDeployment: null,
      azureDalleEndpoint: null,
      hasDalleApiKey: false,
      geminiModel: "gemini-2.5-flash-image",
      hasGeminiApiKey: true,
      imageProvider: "dalle",
      vacationDefaultDays: 30,
      timelineUndoMaxSteps: 50,
      requireMfaForRoles: null,
    });
  });

  it("builds sparse updates and strips secret persistence", () => {
    expect(buildSettingsUpdatePayload({
      aiProvider: "openai",
      azureOpenAiApiKey: "secret",
      smtpPassword: "smtp-secret",
      aiSummaryPrompt: "",
      imageProvider: "gemini",
    })).toEqual({
      data: {
        aiProvider: "openai",
        aiSummaryPrompt: null,
        imageProvider: "gemini",
      },
      ignoredSecretFields: ["azureOpenAiApiKey", "smtpPassword"],
    });
  });

  it("sanitizes sensitive fields in audit snapshots", () => {
    expect(sanitizeSettingsAuditSnapshot({
      azureOpenAiApiKey: "secret",
      smtpPassword: null,
      aiProvider: "azure",
    })).toEqual({
      azureOpenAiApiKey: "***",
      smtpPassword: null,
      aiProvider: "azure",
    });
  });
});
