import { intuneCoverage } from './intuneCatalog.js';

export const psadtSources = [
  { id: 'github', title: 'PSAppDeployToolkit GitHub repository', url: 'https://github.com/psappdeploytoolkit/psappdeploytoolkit' },
  { id: 'new-deployment', title: 'Creating a new deployment', url: 'https://psappdeploytoolkit.com/docs/getting-started/creating-a-new-deployment' },
  { id: 'requirements', title: 'Requirements', url: 'https://psappdeploytoolkit.com/docs/getting-started/requirements' },
  { id: 'release-notes', title: 'Release Notes', url: 'https://psappdeploytoolkit.com/docs/getting-started/release-notes' },
  { id: 'upgrade-v41', title: 'Upgrade Guidance: v4 to v4.1', url: 'https://psappdeploytoolkit.com/docs/getting-started/upgrade-guidance-4x-to-v41' },
  { id: 'faq', title: 'FAQ', url: 'https://psappdeploytoolkit.com/docs/getting-started/faq' },
  { id: 'structure', title: 'Deployment Structure', url: 'https://psappdeploytoolkit.com/docs/deployment-concepts/deployment-structure' },
  { id: 'invoke', title: 'Invoke-AppDeployToolkit', url: 'https://psappdeploytoolkit.com/docs/deployment-concepts/invoke-appdeploytoolkit' },
  { id: 'zero-config', title: 'Zero-Config Deployment', url: 'https://psappdeploytoolkit.com/docs/deployment-concepts/zero-config-deployment' },
  { id: 'how-to-deploy', title: 'How to Deploy', url: 'https://psappdeploytoolkit.com/docs/usage/how-to-deploy' },
  { id: 'ui', title: 'Adding UI Elements', url: 'https://psappdeploytoolkit.com/docs/usage/adding-ui-elements' },
  { id: 'customize', title: 'Customizing Deployments', url: 'https://psappdeploytoolkit.com/docs/usage/customizing-deployments' },
  { id: 'installing', title: 'Installing Applications', url: 'https://psappdeploytoolkit.com/docs/usage/installing-applications' },
  { id: 'admx', title: 'ADMX Templates', url: 'https://psappdeploytoolkit.com/docs/usage/admx/admx-templates' },
  { id: 'exit-codes', title: 'Exit Codes', url: 'https://psappdeploytoolkit.com/docs/4.1.x/reference/exit-codes' }
];

export const psadtModuleMetadata = {
  upstreamRepo: 'https://github.com/psappdeploytoolkit/psappdeploytoolkit',
  moduleVersion: '4.2.0',
  moduleGuid: '8c3c366b-8606-4576-9f2d-4051144f7ca2',
  frontendTemplate: 'src/PSAppDeployToolkit/opt/Frontend/v4/Invoke-AppDeployToolkit.ps1',
  publicFunctionCount: 142,
  admxPolicyCount: 40
};

const publicFunctionNames = `Add-ADTEdgeExtension
Add-ADTFont
Add-ADTModuleCallback
Block-ADTAppExecution
Clear-ADTModuleCallback
Close-ADTInstallationProgress
Close-ADTNotifyIcon
Close-ADTSession
Complete-ADTFunction
Convert-ADTRegistryPath
Convert-ADTValueType
Convert-ADTValuesFromRemainingArguments
ConvertTo-ADTNTAccountOrSID
Copy-ADTContentToCache
Copy-ADTFile
Copy-ADTFileToUserProfiles
Disable-ADTTerminalServerInstallMode
Dismount-ADTWimFile
Enable-ADTTerminalServerInstallMode
Export-ADTEnvironmentTableToSessionState
Get-ADTApplication
Get-ADTBoundParametersAndDefaultValues
Get-ADTCommandTable
Get-ADTConfig
Get-ADTDeferHistory
Get-ADTEnvironmentTable
Get-ADTEnvironmentVariable
Get-ADTExecutableInfo
Get-ADTFileVersion
Get-ADTFreeDiskSpace
Get-ADTIniSection
Get-ADTIniValue
Get-ADTLoggedOnUser
Get-ADTModuleCallback
Get-ADTMsiExitCodeMessage
Get-ADTMsiTableProperty
Get-ADTObjectProperty
Get-ADTOperatingSystemInfo
Get-ADTPEFileArchitecture
Get-ADTPendingReboot
Get-ADTPowerShellProcessPath
Get-ADTPresentationSettingsEnabledUsers
Get-ADTRegistryKey
Get-ADTRunningProcesses
Get-ADTServiceStartMode
Get-ADTSession
Get-ADTShortcut
Get-ADTStringTable
Get-ADTUserNotificationState
Get-ADTUserProfiles
Get-ADTUserToastNotificationMode
Get-ADTWindowTitle
Initialize-ADTFunction
Initialize-ADTModule
Initialize-ADTModuleIfUninitialized
Install-ADTMSUpdates
Install-ADTSCCMSoftwareUpdates
Invoke-ADTAllUsersRegistryAction
Invoke-ADTCommandWithRetries
Invoke-ADTFunctionErrorHandler
Invoke-ADTObjectMethod
Invoke-ADTRegSvr32
Invoke-ADTSCCMTask
Mount-ADTWimFile
New-ADTErrorRecord
New-ADTFolder
New-ADTLogFileName
New-ADTMsiTransform
New-ADTShortcut
New-ADTTemplate
New-ADTValidateScriptErrorRecord
New-ADTZipFile
Open-ADTSession
Out-ADTPowerShellEncodedCommand
Register-ADTDll
Remove-ADTContentFromCache
Remove-ADTDesktopShortcut
Remove-ADTEdgeExtension
Remove-ADTEnvironmentVariable
Remove-ADTFile
Remove-ADTFileFromUserProfiles
Remove-ADTFolder
Remove-ADTFont
Remove-ADTHashtableNullOrEmptyValues
Remove-ADTIniSection
Remove-ADTIniValue
Remove-ADTInvalidFileNameChars
Remove-ADTModuleCallback
Remove-ADTRegistryKey
Reset-ADTDeferHistory
Resolve-ADTErrorRecord
Select-ADTUniqueObject
Send-ADTKeys
Set-ADTActiveSetup
Set-ADTDeferHistory
Set-ADTEnvironmentVariable
Set-ADTIniSection
Set-ADTIniValue
Set-ADTItemPermission
Set-ADTMsiProperty
Set-ADTPowerShellCulture
Set-ADTRegistryKey
Set-ADTServiceStartMode
Set-ADTShortcut
Show-ADTBalloonTip
Show-ADTDialogBox
Show-ADTHelpConsole
Show-ADTInstallationProgress
Show-ADTInstallationPrompt
Show-ADTInstallationRestartPrompt
Show-ADTInstallationWelcome
Show-ADTNotifyIcon
Start-ADTMsiProcess
Start-ADTMsiProcessAsUser
Start-ADTMspProcess
Start-ADTMspProcessAsUser
Start-ADTProcess
Start-ADTProcessAsUser
Start-ADTServiceAndDependencies
Stop-ADTServiceAndDependencies
Test-ADTBattery
Test-ADTCallerIsAdmin
Test-ADTEspActive
Test-ADTMSUpdates
Test-ADTMicrophoneInUse
Test-ADTModuleInitialized
Test-ADTMutexAvailability
Test-ADTNetworkConnection
Test-ADTOobeCompleted
Test-ADTPowerPoint
Test-ADTRegistryValue
Test-ADTServiceExists
Test-ADTSessionActive
Test-ADTUserInFocusMode
Test-ADTUserIsBusy
Unblock-ADTAppExecution
Uninstall-ADTApplication
Unregister-ADTDll
Update-ADTDesktop
Update-ADTEnvironmentPsProvider
Update-ADTGroupPolicy
Write-ADTLogEntry`;

function functionCategory(name) {
  if (/^(Show|Close).*Installation|^Show-ADT|NotifyIcon|Dialog|Balloon|HelpConsole/.test(name)) return 'User Interface';
  if (/^(Start|Install|Uninstall).*Msi|Msp|Process|Application|Updates|SCCM|RegSvr32|Dll/.test(name)) return 'Installer and process';
  if (/Registry|ActiveSetup|Ini|Shortcut|File|Folder|Font|Content|Zip|Wim|EnvironmentVariable/.test(name)) return 'Filesystem registry config';
  if (/Session|Module|Function|Callback|Config|StringTable|CommandTable/.test(name)) return 'Toolkit session';
  if (/User|LoggedOn|PowerPoint|Battery|Network|Oobe|Esp|PendingReboot|OperatingSystem|Service|Mutex|Caller|Microphone|Window|Desktop|GroupPolicy|TerminalServer/.test(name)) return 'Device user state';
  return 'Utilities';
}

export const psadtFunctionCatalog = publicFunctionNames.split('\n').map((name) => ({
  name,
  category: functionCategory(name),
  snippet: `${name} `
}));

export const psadtAdmxPolicies = [
  ['Logo', 'Assets', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Assets'],
  ['LogoDark', 'Assets', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Assets'],
  ['Banner', 'Assets', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Assets'],
  ['TaskbarIcon', 'Assets', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Assets'],
  ['InstallParams', 'MSI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\MSI'],
  ['LoggingOptions', 'MSI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\MSI'],
  ['MutexWaitTime', 'MSI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\MSI'],
  ['SilentParams', 'MSI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\MSI'],
  ['UninstallParams', 'MSI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\MSI'],
  ['CachePath', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['CompanyName', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['CompressLogs', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['FileCopyMode', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogAppend', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogDebugMessage', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogMaxHierarchy', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogMaxHistory', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogMaxSize', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogPath', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogPathNoAdminRights', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogToHierarchy', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogToSubfolder', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogStyle', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogWriteToHost', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['LogHostOutputToStdStreams', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['RegPath', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['RegPathNoAdminRights', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['TempPath', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['TempPathNoAdminRights', 'Toolkit', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\Toolkit'],
  ['BalloonNotifications', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['FluentAccentColor', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['FluentAccentColorDark', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['DialogStyle', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['DefaultExitCode', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['DefaultPromptPersistInterval', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['DefaultTimeout', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['DeferExitCode', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['LanguageOverride', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['PromptToSaveTimeout', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI'],
  ['RestartPromptPersistInterval', 'UI', 'SOFTWARE\\Policies\\PSAppDeployToolkit\\Config\\UI']
].map(([name, category, key]) => ({ name, category, key }));

export const psadtLaunchCommands = [
  { name: 'Install interactive', command: 'Invoke-AppDeployToolkit.exe -DeploymentType Install -DeployMode Interactive' },
  { name: 'Install silent', command: 'Invoke-AppDeployToolkit.exe -DeploymentType Install -DeployMode Silent' },
  { name: 'Uninstall interactive', command: 'Invoke-AppDeployToolkit.exe -DeploymentType Uninstall -DeployMode Interactive' },
  { name: 'Uninstall silent', command: 'Invoke-AppDeployToolkit.exe -DeploymentType Uninstall -DeployMode Silent' },
  { name: 'Repair interactive', command: 'Invoke-AppDeployToolkit.exe -DeploymentType Repair -DeployMode Interactive' },
  { name: 'Repair silent', command: 'Invoke-AppDeployToolkit.exe -DeploymentType Repair -DeployMode Silent' },
  { name: 'Install interactive as SYSTEM for testing', command: 'PsExec64.exe /accepteula /s /i Invoke-AppDeployToolkit.exe -DeploymentType Install -DeployMode Interactive' },
  { name: 'Install silent as SYSTEM for testing', command: 'PsExec64.exe /accepteula /s Invoke-AppDeployToolkit.exe -DeploymentType Install -DeployMode Silent' }
];

export const psadtSupportMatrix = [
  { area: 'Deployment creation', sourceId: 'new-deployment', support: 'profile', status: 'supported', implementation: 'PSADT profile wizard can create v4/v3-compatible script skeletons and script library entries.' },
  { area: 'Requirements', sourceId: 'requirements', support: 'verifier', status: 'supported', implementation: 'Verifier flags scripts that declare unsupported PowerShell versions and records PSADT platform requirements.' },
  { area: 'Release-note gotchas', sourceId: 'release-notes', support: 'verifier', status: 'supported', implementation: 'Verifier checks known gotchas such as ServiceUI usage, Fluent UI prompt icon usage, raw MSI handling, and old deployment names.' },
  { area: 'v4 to v4.1 upgrade guidance', sourceId: 'upgrade-v41', support: 'verifier', status: 'supported', implementation: 'Verifier checks RequireAdmin/AppProcessesToClose placement, removed config keys, session cleanup, and deprecated patterns.' },
  { area: 'FAQ guidance', sourceId: 'faq', support: 'verifier', status: 'supported', implementation: 'Verifier catches missing ADTSession assumptions, global module imports, Intune defer retry concerns, and user-context RequireAdmin guidance.' },
  { area: 'Upstream module coverage', sourceId: 'github', support: 'catalog', status: 'supported', implementation: 'Catalog tracks the upstream 4.2.0 module metadata, all 142 public functions, launch helpers, and 40 ADMX policies.' },
  { area: 'Template structure', sourceId: 'structure', support: 'reference', status: 'supported', implementation: 'Structure table tracks Invoke-AppDeployToolkit.ps1, PSAppDeployToolkit, Files, SupportFiles, Assets, Config, and Strings.' },
  { area: 'ADTSession properties', sourceId: 'invoke', support: 'profile', status: 'supported', implementation: 'Profiles store app metadata, success/reboot codes, RequireAdmin, and close-process definitions.' },
  { area: 'Install/Uninstall/Repair phases', sourceId: 'invoke', support: 'generator', status: 'supported', implementation: 'Renderer emits Install-ADTDeployment, Uninstall-ADTDeployment, and Repair-ADTDeployment phase blocks.' },
  { area: 'Zero-config MSI', sourceId: 'zero-config', support: 'profile', status: 'supported', implementation: 'Zero-config toggle emits an empty AppName and a Files-folder checklist for single MSI/MST/MSP/WIM packages.' },
  { area: 'Deployment commands', sourceId: 'how-to-deploy', support: 'generator', status: 'supported', implementation: 'Renderer produces Invoke-AppDeployToolkit.exe and PowerShell command lines for Intune/package documentation.' },
  { area: 'UI elements', sourceId: 'ui', support: 'snippets', status: 'supported', implementation: 'Snippet library covers welcome, progress, prompts, restart prompts, dialog boxes, and help console.' },
  { area: 'Customization', sourceId: 'customize', support: 'checklist', status: 'supported', implementation: 'Profiles carry config, strings, assets, log, and CompressLogs guidance for package readiness.' },
  { area: 'Application installers', sourceId: 'installing', support: 'snippets', status: 'supported', implementation: 'Snippet library covers Start-ADTProcess, Start-ADTProcessAsUser, and Start-ADTMsiProcess.' },
  { area: 'ADMX / Intune policy', sourceId: 'admx', support: 'checklist', status: 'supported', implementation: 'ADMX checklist tracks policy files, Intune import, Central Store, and HKLM policy path.' }
  ,{ area: 'Exit codes', sourceId: 'exit-codes', support: 'verifier', status: 'supported', implementation: 'Verifier flags reserved built-in exit code ranges and recommends custom script/extension ranges.' }
];

export const psadtStructureRows = [
  { path: 'Invoke-AppDeployToolkit.ps1', purpose: 'Main deployment script and phase logic.', required: true },
  { path: 'Invoke-AppDeployToolkit.exe', purpose: 'Wrapper executable for hidden PowerShell launches and command-line options.', required: false },
  { path: 'PSAppDeployToolkit/', purpose: 'Core module files; should not be modified in deployments.', required: true },
  { path: 'PSAppDeployToolkit.Extensions/', purpose: 'Optional custom functions/extensions.', required: false },
  { path: 'Files/', purpose: 'Installer media such as EXE, MSI, MST, MSP, WIM, archives, and configs.', required: true },
  { path: 'SupportFiles/', purpose: 'Registry files, configuration templates, shortcuts, docs, licenses, and support payloads.', required: false },
  { path: 'Assets/', purpose: 'AppIcon.png, classic banners, logos, and UI branding images.', required: false },
  { path: 'Config/config.psd1', purpose: 'Toolkit behavior, MSI parameters, logging, UI, security, and performance settings.', required: true },
  { path: 'Strings/strings.psd1', purpose: 'Localized UI strings and deployment messaging.', required: true },
  { path: 'PSAppDeployToolkit/ADMX/', purpose: 'PSADT ADMX/ADML policy templates for Group Policy or Intune.', required: false }
];

export const psadtInvokeParameters = [
  { name: '-DeploymentType', values: ['Install', 'Uninstall', 'Repair'], description: 'Selects install, uninstall, or repair deployment flow.' },
  { name: '-DeployMode', values: ['Interactive', 'Silent', 'NonInteractive'], description: 'Controls UI behavior and whether interactive prompts are allowed.' },
  { name: '-SuppressRebootPassThru', values: ['switch'], description: 'Suppresses 3010 reboot-required passthrough exit codes.' },
  { name: '-TerminalServerMode', values: ['switch'], description: 'Changes to user install mode for RDS/Citrix systems.' },
  { name: '-DisableLogging', values: ['switch'], description: 'Disables file logging.' },
  { name: '/Debug', values: ['exe only'], description: 'Opens a visible debug window with live logging.' },
  { name: '/32', values: ['exe only'], description: 'Launches with x86 PowerShell.' },
  { name: '/Core', values: ['exe only'], description: 'Launches with PowerShell Core.' },
  { name: '-File', values: ['*.ps1'], description: 'Runs a custom script file instead of Invoke-AppDeployToolkit.ps1.' }
];

export const psadtSnippets = [
  {
    id: 'welcome-close-processes',
    title: 'Welcome prompt with close processes',
    category: 'UI',
    functionName: 'Show-ADTInstallationWelcome',
    body: "Show-ADTInstallationWelcome -CloseProcesses $adtSession.AppProcessesToClose -AllowDefer -DeferTimes 3 -CheckDiskSpace"
  },
  {
    id: 'progress',
    title: 'Installation progress',
    category: 'UI',
    functionName: 'Show-ADTInstallationProgress',
    body: "Show-ADTInstallationProgress -StatusMessage 'Installing application. Please wait...'"
  },
  {
    id: 'custom-prompt',
    title: 'Custom installation prompt',
    category: 'UI',
    functionName: 'Show-ADTInstallationPrompt',
    body: "Show-ADTInstallationPrompt -Title 'Deployment notice' -Message 'The application will now be installed.' -ButtonRightText 'OK'"
  },
  {
    id: 'restart-prompt',
    title: 'Restart prompt',
    category: 'UI',
    functionName: 'Show-ADTInstallationRestartPrompt',
    body: 'Show-ADTInstallationRestartPrompt -CountdownSeconds 600 -CountdownNoHideSeconds 60'
  },
  {
    id: 'dialog',
    title: 'Generic dialog box',
    category: 'UI',
    functionName: 'Show-ADTDialogBox',
    body: "Show-ADTDialogBox -Text 'Deployment completed.' -Icon Information -Buttons OK"
  },
  {
    id: 'help-console',
    title: 'Toolkit help console',
    category: 'UI',
    functionName: 'Show-ADTHelpConsole',
    body: 'Show-ADTHelpConsole'
  },
  {
    id: 'start-process',
    title: 'Install EXE',
    category: 'Install',
    functionName: 'Start-ADTProcess',
    body: "Start-ADTProcess -FilePath 'setup.exe' -ArgumentList '/S' -WaitForChildProcesses -SuccessExitCodes @(0, 3010)"
  },
  {
    id: 'start-process-user',
    title: 'Run process as active user',
    category: 'Install',
    functionName: 'Start-ADTProcessAsUser',
    body: "Start-ADTProcessAsUser -FilePath '%LOCALAPPDATA%\\Programs\\SomeApp\\Uninstall.exe' -ArgumentList '/S' -ExpandEnvironmentVariables"
  },
  {
    id: 'start-msi',
    title: 'Install MSI with transform',
    category: 'Install',
    functionName: 'Start-ADTMsiProcess',
    body: "Start-ADTMsiProcess -Action 'Install' -FilePath 'SomeApp.msi' -Transforms 'SomeApp.mst' -AdditionalArgumentList 'SERIAL=12345' -SecureArgumentList"
  }
];

export function psadtCatalog() {
  return {
    module: psadtModuleMetadata,
    sources: psadtSources,
    supportMatrix: psadtSupportMatrix,
    structure: psadtStructureRows,
    invokeParameters: psadtInvokeParameters,
    functions: psadtFunctionCatalog,
    admxPolicies: psadtAdmxPolicies,
    launchCommands: psadtLaunchCommands,
    intune: intuneCoverage,
    snippets: psadtSnippets
  };
}
