export type SettingsSectionId =
  | "general-settings"
  | "appearance-settings"
  | "dashboard-settings"
  | "credentials-settings"
  | "assistant-settings"
  | "ssh-settings"
  | "terminal-settings"
  | "url-settings"
  | "rdp-settings"
  | "vnc-settings"
  | "about-settings";

export type SettingsAssistantContext = {
  contextKind: "settings";
  contextLabel: string;
  connectionLabel: string;
  sourceLabel: string;
  text: string;
};

export type SettingsTutorialTarget = {
  targetId: string;
  titleKey: string;
  bodyKey: string;
};

type Translate = (key: string, fallback: string) => string;

type SettingsControlSummary = {
  key: string;
  description: string;
  tutorialTargetId?: string;
};

type SettingsSectionSummary = {
  labelKey: string;
  fallbackLabel: string;
  controls: SettingsControlSummary[];
};

const SETTINGS_SECTIONS: Record<SettingsSectionId, SettingsSectionSummary> = {
  "general-settings": {
    labelKey: "settings.sectionGeneral",
    fallbackLabel: "General",
    controls: [
      {
        key: "settings.language",
        description: "UI language selector.",
        tutorialTargetId: "settings.language",
      },
      {
        key: "settings.workspaceAccess",
        description: "Workspace access and runtime toggles.",
        tutorialTargetId: "settings.workspaceAccess",
      },
      {
        key: "settings.useDirectxScreenCapture",
        description: "DXGI Desktop Duplication screenshot acceleration toggle.",
        tutorialTargetId: "settings.useDirectxScreenCapture",
      },
      {
        key: "settings.statusBar",
        description: "Status Bar CPU/RAM/Network monitor visibility and polling interval.",
        tutorialTargetId: "settings.statusBar",
      },
      { key: "settings.autoStartWithWindows", description: "Launch KKTerm minimized when the user signs in to Windows." },
      {
        key: "settings.settingsData",
        description: "Export, import, database folder, and reset actions.",
        tutorialTargetId: "settings.settingsData",
      },
      {
        key: "settings.advancedDebugging",
        description: "Advanced Debugging log toggle for full AI Assistant debug logs.",
        tutorialTargetId: "settings.debug",
      },
    ],
  },
  "appearance-settings": {
    labelKey: "settings.sectionAppearance",
    fallbackLabel: "Appearance",
    controls: [
      {
        key: "settings.appUiFontFamily",
        description: "App UI font family selector.",
        tutorialTargetId: "settings.appUiFontFamily",
      },
      {
        key: "settings.colorScheme",
        description: "App color scheme selector and preview swatches.",
        tutorialTargetId: "settings.appearance.colorScheme",
      },
      {
        key: "settings.resetLayout",
        description: "Workspace chrome and pane layout reset action.",
        tutorialTargetId: "settings.resetLayout",
      },
    ],
  },
  "dashboard-settings": {
    labelKey: "settings.sectionDashboard",
    fallbackLabel: "Dashboard",
    controls: [
      {
        key: "settings.dashboardDefaultLanding",
        description: "Default landing view behavior.",
        tutorialTargetId: "settings.dashboardDefaultLanding",
      },
      {
        key: "settings.dashboardUseRandomDynamicBackground",
        description: "Automatically assign a random dynamic background to new Dashboard Views.",
        tutorialTargetId: "settings.dashboardUseRandomDynamicBackground",
      },
      {
        key: "settings.dashboardMaxActiveScriptWidgets",
        description: "Active script widget iframe cap.",
        tutorialTargetId: "settings.dashboardMaxActiveScriptWidgets",
      },
    ],
  },
  "credentials-settings": {
    labelKey: "settings.sectionCredentials",
    fallbackLabel: "Credentials",
    controls: [
      {
        key: "settings.credentialsStored",
        description: "Stored credential summaries and delete actions.",
        tutorialTargetId: "settings.credentialsStored",
      },
      {
        key: "settings.widgetCredentialsStored",
        description: "Stored Dashboard widget secrets.",
        tutorialTargetId: "settings.widgetCredentialsStored",
      },
    ],
  },
  "assistant-settings": {
    labelKey: "settings.sectionAiAssistant",
    fallbackLabel: "AI Assistant",
    controls: [
      {
        key: "settings.aiProvider",
        description: "AI provider and model selection.",
        tutorialTargetId: "settings.aiProvider",
      },
      {
        key: "settings.aiToolsTitle",
        description: "Assistant tool enablement toggles.",
        tutorialTargetId: "settings.aiToolsTitle",
      },
      {
        key: "settings.aiCustomInstructions",
        description: "Assistant custom instruction text.",
        tutorialTargetId: "settings.aiCustomInstructions",
      },
      {
        key: "settings.assistantSkillsTitle",
        description: "Local Assistant Skills management.",
        tutorialTargetId: "settings.assistantSkillsTitle",
      },
      {
        key: "settings.mcpServersTitle",
        description: "MCP server management.",
        tutorialTargetId: "settings.mcpServersTitle",
      },
    ],
  },
  "ssh-settings": {
    labelKey: "settings.sectionSsh",
    fallbackLabel: "SSH",
    controls: [
      {
        key: "settings.defaultUser",
        description: "Default SSH username.",
        tutorialTargetId: "settings.defaultUser",
      },
      {
        key: "settings.defaultPort",
        description: "Default SSH port.",
        tutorialTargetId: "settings.defaultPort",
      },
      {
        key: "settings.defaultKey",
        description: "Default SSH identity file.",
        tutorialTargetId: "settings.defaultKey",
      },
      {
        key: "settings.sshBufferLines",
        description: "SSH terminal buffer behavior.",
        tutorialTargetId: "settings.sshBufferLines",
      },
    ],
  },
  "terminal-settings": {
    labelKey: "settings.sectionTerminal",
    fallbackLabel: "Terminal",
    controls: [
      {
        key: "settings.terminalFontFamily",
        description: "Local terminal font.",
        tutorialTargetId: "settings.terminalFontFamily",
      },
      {
        key: "settings.terminalFontSize",
        description: "Local terminal font size.",
        tutorialTargetId: "settings.terminalFontSize",
      },
      {
        key: "settings.defaultShell",
        description: "Default local shell.",
        tutorialTargetId: "settings.defaultShell",
      },
      {
        key: "settings.scrollbackLines",
        description: "Terminal scrollback line count.",
        tutorialTargetId: "settings.scrollbackLines",
      },
    ],
  },
  "url-settings": {
    labelKey: "settings.sectionUrl",
    fallbackLabel: "URL",
    controls: [
      {
        key: "settings.ignoreCertificateErrors",
        description: "URL Connection certificate handling.",
        tutorialTargetId: "settings.ignoreCertificateErrors",
      },
      {
        key: "settings.urlSavedPasswords",
        description: "Saved website password metadata.",
        tutorialTargetId: "settings.urlSavedPasswords",
      },
      {
        key: "settings.urlDataShards",
        description: "URL data shard management.",
        tutorialTargetId: "settings.urlDataShards",
      },
    ],
  },
  "rdp-settings": {
    labelKey: "settings.sectionRdp",
    fallbackLabel: "Remote Desktop",
    controls: [
      {
        key: "settings.rdpColorDepth",
        description: "RDP color depth default.",
        tutorialTargetId: "settings.rdpColorDepth",
      },
      {
        key: "settings.rdpPerformanceProfile",
        description: "RDP performance profile default.",
        tutorialTargetId: "settings.rdpPerformanceProfile",
      },
    ],
  },
  "vnc-settings": {
    labelKey: "settings.sectionVnc",
    fallbackLabel: "VNC",
    controls: [
      {
        key: "settings.vncViewOnly",
        description: "VNC view-only default.",
        tutorialTargetId: "settings.vncViewOnly",
      },
      {
        key: "settings.vncColorLevel",
        description: "VNC color level default.",
        tutorialTargetId: "settings.vncColorLevel",
      },
    ],
  },
  "about-settings": {
    labelKey: "settings.sectionAbout",
    fallbackLabel: "About",
    controls: [
      {
        key: "settings.aboutVersion",
        description: "Product version and open-source component information.",
        tutorialTargetId: "settings.aboutVersion",
      },
    ],
  },
};

export function buildSettingsAssistantContext(
  sectionId: SettingsSectionId,
  translate: Translate = (_key, fallback) => fallback,
): SettingsAssistantContext {
  const section = SETTINGS_SECTIONS[sectionId];
  const settingsLabel = translate("settings.title", "Settings");
  const sectionLabel = translate(section.labelKey, section.fallbackLabel);
  const controls = section.controls
    .map((control) => {
      const tutorial = control.tutorialTargetId
        ? ` Tutorial target: ${control.tutorialTargetId}.`
        : "";
      return `- ${control.key}: ${control.description}${tutorial}`;
    })
    .join("\n");

  return {
    contextKind: "settings",
    contextLabel: `${settingsLabel} - ${sectionLabel}`,
    connectionLabel: translate("ai.settingsContextLabel", "Current Settings page"),
    sourceLabel: `${settingsLabel} context`,
    text: [
      `Active Settings section: ${sectionLabel} (${sectionId}).`,
      "Visible controls and i18n keys:",
      controls,
      "For UI help, answer first and offer to navigate when a matching tutorial target is listed. Only call tutorial_highlight after the user accepts; include navigation if the target belongs to another app surface.",
    ].join("\n"),
  };
}

export function settingsTutorialTargetForPrompt(
  prompt: string,
  sectionId: SettingsSectionId,
): SettingsTutorialTarget | undefined {
  const normalized = prompt.toLowerCase();
  if (
    sectionId === "appearance-settings" &&
    /\b(colou?r|theme|scheme|appearance)\b/.test(normalized)
  ) {
    return {
      targetId: "settings.appearance.colorScheme",
      titleKey: "ai.tutorial.colorSchemeTitle",
      bodyKey: "ai.tutorial.colorSchemeBody",
    };
  }
  if (
    sectionId === "general-settings" &&
    /\b(language|locale|translation|translate|english|chinese|japanese|korean|thai|spanish|french|german|portuguese|indonesian)\b/.test(normalized)
  ) {
    return {
      targetId: "settings.language",
      titleKey: "settings.language",
      bodyKey: "settings.language",
    };
  }
  return undefined;
}
