#define MyAppName "AWatch-rus InstallKit"
#define MyAppVersion "1.0.0"
#define MyAppPublisher "AWatch-rus"

#define AwDefaultServerHost "aw-server"
#define AwDefaultServerPort "5600"
#define AwDefaultWorktimeReportBase "http://aw-server:5610"
#define AwDefaultWorktimeHost "SHARKON2025"
#define AwDefaultUsers "user1,user2,user3,user4,user5"
#define AwDefaultInstallRoot "C:\\Program Files\\AWatch-rus\\bin"
#define AwDefaultStateRoot "C:\\ProgramData\\AWatch-rus"
#define AwDefaultZipName "activitywatch-v0.13.2-windows-x86_64.zip"

; This installer wraps the standalone-service path.
; It is suitable for standalone/headless deployment and must not be treated
; as the canonical multi-user RDP deployment path used on SHARKON2025.

[Setup]
AppId={{6D6A1F74-0F4F-4A57-B5E3-1C2C2F56C0E9}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={autopf}\AWatch-rus
DefaultGroupName=AWatch-rus
OutputDir=.
OutputBaseFilename=AWatch-rus-InstallKit
Compression=lzma
SolidCompression=yes
ArchitecturesInstallIn64BitMode=x64compatible
PrivilegesRequired=admin

[Languages]
Name: "russian"; MessagesFile: "compiler:Languages\Russian.isl"

[Tasks]
Name: "deploy"; Description: "Запустить деплой после установки"; Flags: checkedonce
Name: "validate"; Description: "Запустить validate-deployment (через -ValidateAfterDeploy)"; Flags: checkedonce

[Files]
Source: "..\..\ActivityWatch.Windows.Common.psd1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\ActivityWatch.Windows.Common.psm1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\install-standalone-service.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\aw-standalone-service.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\deploy-single-user.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\deploy-domain-users.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\deploy-ensemble.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\AWatchRusCollectorGuardService.cs"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\install-collector-guard-service.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\..\adk-rust\target\x86_64-pc-windows-gnu\release\aw-windows-telemetry.exe"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\hardening-recovery.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\validate-deployment.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\migrate-awatch-rus-paths.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\worktime-session-collector.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\email-outbound-collector.ps1"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\web-category-rules.example.json"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\dlp-policy.example.json"; DestDir: "{app}\windows"; Flags: ignoreversion
Source: "..\..\dlp-policy.native-cross-os.example.json"; DestDir: "{app}\windows"; Flags: ignoreversion
; Offline payload (optional): place ZIP into windows/installkit/innosetup/payload/ before compiling.
Source: "payload\{#AwDefaultZipName}"; DestDir: "{app}\payload"; Flags: ignoreversion skipifsourcedoesntexist
Source: "innosetup-rdp-package-filelist.md"; DestDir: "{app}\windows\installkit\innosetup"; Flags: ignoreversion

[Run]
Filename: "powershell.exe"; Parameters: "{code:GetStandaloneInstallParams}"; Flags: runhidden; Tasks: deploy

[Code]
var
  ServerHostPage: TInputQueryWizardPage;

procedure InitializeWizard;
begin
  ServerHostPage := CreateInputQueryPage(
    wpSelectDir,
    'Параметры AW сервера',
    'Укажите сервер ActivityWatch (куда агенты будут отправлять данные).',
    'Если нужно, измените host/port. По умолчанию — наша конфигурация.'
  );
  { Worktime CSV/JSON reports are served by aw-worktime-api on :5610 (AwDefaultWorktimeReportBase).
    Standard AW "Сегодня" is backed by server-side aw-worktime-ui-bridge timer on AW host (AwDefaultWorktimeHost). }
  ServerHostPage.Add('ServerHost', False);
  ServerHostPage.Add('ServerPort', False);
  ServerHostPage.Values[0] := '{#AwDefaultServerHost}';
  ServerHostPage.Values[1] := '{#AwDefaultServerPort}';
end;

function GetStandaloneInstallParams(Param: string): string;
var
  serverHost: string;
  serverPort: string;
begin
  serverHost := Trim(ServerHostPage.Values[0]);
  serverPort := Trim(ServerHostPage.Values[1]);
  if serverHost = '' then
    RaiseException('ServerHost is empty.');
  if serverPort = '' then
    RaiseException('ServerPort is empty.');

  Result :=
    '-NoProfile -ExecutionPolicy Bypass -File "' + ExpandConstant('{app}\windows\install-standalone-service.ps1') + '"' +
    ' -ServerHost "' + serverHost + '"' +
    ' -ServerPort ' + serverPort +
    ' -InstallRoot "{#AwDefaultInstallRoot}"' +
    ' -StateRoot "{#AwDefaultStateRoot}"';
end;
