/**
 * electron-builder afterPack hook — runs after app is packed but before
 * DMG/installer creation. Ad-hoc signs macOS apps so Gatekeeper shows
 * "unidentified developer" (bypassable via right-click → Open) instead
 * of "damaged and can't be opened" (requires xattr -cr in Terminal).
 */
const { execSync } = require('child_process');
const path = require('path');

exports.default = async function afterPack(context) {
  if (context.electronPlatformName !== 'darwin') return;

  const appName = context.packager.appInfo.productFilename;
  const appPath = path.join(context.appOutDir, `${appName}.app`);

  console.log(`  [afterPack] Ad-hoc signing: ${appPath}`);
  try {
    execSync(`codesign --force --deep -s - "${appPath}"`, { stdio: 'pipe' });
    console.log(`  [afterPack] Signed successfully`);
  } catch (err) {
    console.warn(`  [afterPack] Signing failed (non-fatal): ${err.message}`);
  }
};
