import type { ActivePage } from "./ActivityRail";
import type { SettingsSectionId } from "../modules/settings/settingsAssistantContext";

export type TutorialNavigationTarget = {
  page: ActivePage;
  settingsSectionId?: SettingsSectionId;
};

const SETTINGS_SECTION_IDS = new Set<SettingsSectionId>([
  "general-settings",
  "appearance-settings",
  "dashboard-settings",
  "credentials-settings",
  "assistant-settings",
  "ssh-settings",
  "terminal-settings",
  "url-settings",
  "rdp-settings",
  "vnc-settings",
  "about-settings",
]);

const SETTINGS_TUTORIAL_TARGET_SECTIONS: Record<string, SettingsSectionId> = {
  "settings.language": "general-settings",
  "settings.workspaceAccess": "general-settings",
  "settings.useDirectxScreenCapture": "general-settings",
  "settings.statusBar": "general-settings",
  "settings.settingsData": "general-settings",
  "settings.debug": "general-settings",
  "settings.appUiFontFamily": "appearance-settings",
  "settings.appearance.colorScheme": "appearance-settings",
  "settings.resetLayout": "appearance-settings",
  "settings.dashboardDefaultLanding": "dashboard-settings",
  "settings.dashboardUseRandomDynamicBackground": "dashboard-settings",
  "settings.dashboardMaxActiveScriptWidgets": "dashboard-settings",
  "settings.credentialsStored": "credentials-settings",
  "settings.widgetCredentialsStored": "credentials-settings",
  "settings.aiProvider": "assistant-settings",
  "settings.aiToolsTitle": "assistant-settings",
  "settings.aiCustomInstructions": "assistant-settings",
  "settings.assistantSkillsTitle": "assistant-settings",
  "settings.mcpServersTitle": "assistant-settings",
  "settings.defaultUser": "ssh-settings",
  "settings.defaultPort": "ssh-settings",
  "settings.defaultKey": "ssh-settings",
  "settings.sshBufferLines": "ssh-settings",
  "settings.terminalFontFamily": "terminal-settings",
  "settings.terminalFontSize": "terminal-settings",
  "settings.defaultShell": "terminal-settings",
  "settings.scrollbackLines": "terminal-settings",
  "settings.ignoreCertificateErrors": "url-settings",
  "settings.urlSavedPasswords": "url-settings",
  "settings.urlDataShards": "url-settings",
  "settings.rdpColorDepth": "rdp-settings",
  "settings.rdpPerformanceProfile": "rdp-settings",
  "settings.rdpRemoteResolution": "rdp-settings",
  "settings.vncViewOnly": "vnc-settings",
  "settings.vncColorLevel": "vnc-settings",
  "settings.aboutVersion": "about-settings",
};

const WORKSPACE_TUTORIAL_TARGET_IDS = [
  "app.activityRailWorkspace",
  "app.activityRailDashboard",
  "app.connectionRail",
  "app.activityRailDontSleep",
  "app.activityRailSettings",
  "app.connectionsResize",
  "app.aiAssistantResize",
  "connections.panel",
  "connections.search",
  "connections.quickConnect",
  "connections.addConnection",
  "connections.folderControls",
  "connections.tree",
  "workspace.tabStrip",
  "workspace.canvas",
  "workspace.emptyState",
  "workspace.statusBar",
  "workspace.hostUsage",
  "workspace.screenshotMenu",
  "terminal.pane",
  "terminal.tmuxSessions",
  "terminal.sshPortRedirect",
  "terminal.startRecording",
  "terminal.openSftp",
  "terminal.copySelection",
  "terminal.sendToAi",
  "terminal.actions",
  "terminal.searchBar",
  "terminal.surface",
  "sftp.toolbar",
  "sftp.upload",
  "sftp.download",
  "sftp.terminal",
  "sftp.localPane",
  "sftp.remotePane",
  "sftp.transferQueue",
  "webview.toolbar",
  "webview.address",
  "webview.openExternally",
  "webview.autoRefresh",
  "webview.savePassword",
  "webview.fillCredential",
  "webview.surface",
  "remoteDesktop.toolbar",
  "remoteDesktop.sendCtrlAltDel",
  "remoteDesktop.reconnect",
  "remoteDesktop.sendToAi",
  "remoteDesktop.surface",
] as const;

const TUTORIAL_TARGET_NAVIGATION: Record<string, TutorialNavigationTarget> = {
  ...Object.fromEntries(
    WORKSPACE_TUTORIAL_TARGET_IDS.map((targetId) => [
      targetId,
      { page: "workspace" },
    ]),
  ),
  ...Object.fromEntries(
    Object.entries(SETTINGS_TUTORIAL_TARGET_SECTIONS).map(
      ([targetId, settingsSectionId]) => [
        targetId,
        { page: "settings", settingsSectionId },
      ],
    ),
  ),
};

export function tutorialNavigationForTarget(
  targetId: string,
): TutorialNavigationTarget | undefined {
  return TUTORIAL_TARGET_NAVIGATION[targetId.trim()];
}

export function normalizeTutorialNavigationTarget(
  value: unknown,
): TutorialNavigationTarget | undefined {
  if (!value || typeof value !== "object") {
    return undefined;
  }

  const candidate = value as Record<string, unknown>;
  const page = normalizeTutorialPage(candidate.page);
  const settingsSectionId = normalizeSettingsSectionId(candidate.settingsSectionId);

  if (candidate.settingsSectionId !== undefined && !settingsSectionId) {
    return undefined;
  }

  if (page) {
    if (page !== "settings" && settingsSectionId) {
      return undefined;
    }
    return settingsSectionId ? { page, settingsSectionId } : { page };
  }

  return settingsSectionId ? { page: "settings", settingsSectionId } : undefined;
}

function normalizeTutorialPage(value: unknown): ActivePage | undefined {
  if (value === "workspace" || value === "dashboard" || value === "settings") {
    return value;
  }
  return undefined;
}

function normalizeSettingsSectionId(value: unknown): SettingsSectionId | undefined {
  if (typeof value !== "string") {
    return undefined;
  }
  const trimmed = value.trim() as SettingsSectionId;
  return SETTINGS_SECTION_IDS.has(trimmed) ? trimmed : undefined;
}
