<?xml version="1.0" encoding="UTF-8"?>
<!--
  MCPProxy Windows Installer - WiX Toolset 4.x Definition (amd64)
  This installer packages both mcpproxy.exe (core server) and mcpproxy-tray.exe (GUI application)
  for Windows 10 version 21H2+ and Windows 11 (amd64 architecture)
-->
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  <Package
    Name="MCPProxy"
    Manufacturer="Smart MCP Proxy"
    Version="!(bind.FileVersion.mcpproxy.exe)"
    UpgradeCode="A1B2C3D4-E5F6-7890-ABCD-EF1234567890"
    Language="1033"
    InstallerVersion="500"
    Compressed="yes">

    <!-- Product metadata -->
    <SummaryInformation
      Description="MCPProxy - Smart MCP Proxy for AI Agents"
      Manufacturer="Smart MCP Proxy"
      Keywords="MCP,Proxy,AI,Agent" />

    <!-- System requirements: Windows 10 21H2+ (10.0.19044) -->
    <Launch
      Condition="(VersionNT &gt;= 603) OR (VersionNT &gt;= 602 AND VersionNT64)"
      Message="This application requires Windows 10 version 21H2 or later." />

    <!-- Major upgrade logic (in-place upgrades per FR-019) -->
    <MajorUpgrade
      DowngradeErrorMessage="A newer version of [ProductName] is already installed."
      Schedule="afterInstallInitialize"
      AllowSameVersionUpgrades="yes" />

    <!-- Embedded UI (standard dialog set) -->
    <WixVariable Id="WixUILicenseRtf" Value="LICENSE.rtf" />
    <UIRef Id="WixUI_InstallDir" />
    <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />

    <!-- Application icon for Add/Remove Programs -->
    <Icon Id="mcpproxy.ico" SourceFile="$(var.BinPath)\mcpproxy-tray.exe" />
    <Property Id="ARPPRODUCTICON" Value="mcpproxy.ico" />

    <!-- Program Files installation directory -->
    <StandardDirectory Id="ProgramFiles64Folder">
      <Directory Id="INSTALLFOLDER" Name="MCPProxy">
        <!-- Core binary component -->
        <Component Id="mcpproxy.exe.Component" Guid="B1C2D3E4-F5A6-7B89-CDEF-1234567890AB">
          <File
            Id="mcpproxy.exe"
            Source="$(var.BinPath)\mcpproxy.exe"
            KeyPath="yes" />
        </Component>

        <!-- Tray application component -->
        <Component Id="mcpproxy_tray.exe.Component" Guid="C2D3E4F5-A6B7-89CD-EF12-34567890ABCD">
          <File
            Id="mcpproxy_tray.exe"
            Source="$(var.BinPath)\mcpproxy-tray.exe"
            KeyPath="yes" />
        </Component>

        <!-- System PATH component (US2 - FR-003) -->
        <Component Id="SystemPath.Component" Guid="D3E4F5A6-B7C8-9DEF-1234-567890ABCDEF">
          <Environment Id="PATH" Name="PATH" Value="[INSTALLFOLDER]" Permanent="no" Part="last" Action="set" System="yes" />
        </Component>
      </Directory>
    </StandardDirectory>

    <!-- Start Menu shortcuts (FR-004) -->
    <StandardDirectory Id="ProgramMenuFolder">
      <Directory Id="ApplicationProgramsFolder" Name="MCPProxy">
        <Component Id="StartMenuShortcut.Component" Guid="E4F5A6B7-C8D9-EF12-3456-7890ABCDEF12">
          <Shortcut
            Id="mcpproxy_tray.Shortcut"
            Name="MCPProxy Tray"
            Target="[INSTALLFOLDER]mcpproxy-tray.exe"
            WorkingDirectory="INSTALLFOLDER"
            Icon="mcpproxy.ico" />
          <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall" />
          <RegistryValue Root="HKCU" Key="Software\Smart MCP Proxy\MCPProxy" Name="installed" Type="integer" Value="1" KeyPath="yes" />
        </Component>
      </Directory>
    </StandardDirectory>

    <!-- Feature: Complete installation -->
    <Feature Id="CompleteFeature" Title="MCPProxy Complete" Level="1">
      <ComponentRef Id="mcpproxy.exe.Component" />
      <ComponentRef Id="mcpproxy_tray.exe.Component" />
      <ComponentRef Id="StartMenuShortcut.Component" />
      <ComponentRef Id="SystemPath.Component" />
    </Feature>

    <!-- Custom action for post-install launch (FR-005, US3) -->
    <CustomAction
      Id="LaunchTrayApplication"
      Directory="INSTALLFOLDER"
      ExeCommand="[INSTALLFOLDER]mcpproxy-tray.exe"
      Execute="immediate"
      Impersonate="yes"
      Return="asyncNoWait" />

    <!-- Launch action UI sequence (only in interactive mode) -->
    <UI>
      <Publish Dialog="ExitDialog" Control="Finish" Event="DoAction" Value="LaunchTrayApplication">
        NOT Installed AND WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1
      </Publish>
    </UI>

    <!-- Optional checkbox on exit dialog -->
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch MCPProxy Tray" />
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1" />
  </Package>
</Wix>
