#!/usr/bin/env node
/**
 * One-shot local Plaza setup:
 *   1. /etc/hosts  plaza.tkmind.cn -> 127.0.0.1
 *   2. TLS cert for https://plaza.tkmind.cn
 *
 * Usage: sudo node scripts/setup-plaza-local.mjs
 */
import { spawnSync } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');

if (process.getuid?.() !== 0) {
  console.log('需要 root 权限，请执行：');
  console.log('  sudo pnpm setup:plaza-local');
  process.exit(1);
}

const dns = spawnSync('node', [path.join(root, 'scripts/setup-plaza-local-dns.mjs')], {
  stdio: 'inherit',
  env: process.env,
});
if (dns.status !== 0) process.exit(dns.status ?? 1);

const tls = spawnSync('node', [path.join(root, 'scripts/plaza-local-tls.mjs')], {
  stdio: 'inherit',
  env: process.env,
});
if (tls.status !== 0) process.exit(tls.status ?? 1);

console.log('');
console.log('本地 Plaza 已配置。启动：');
console.log('  pnpm dev:plaza          # Plaza + Portal');
console.log('  sudo pnpm dev:plaza-proxy   # HTTPS :443 代理');
console.log('  或 pnpm dev             # 全栈');
console.log('');
console.log('访问：https://plaza.tkmind.cn/plaza');
