#!/usr/bin/env bun
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs'

const DEV_VITE_SCRIPT = 'concurrently "vite" "json-server mock/db.json --port 3001 --delay 100"'
const DB_PATH = 'mock/db.json'

// --- Patch package.json ---
const pkgRaw = readFileSync('package.json', 'utf-8')
const pkg = JSON.parse(pkgRaw)

if (pkg.scripts?.['dev:vite']) {
  console.log('package.json already has dev:vite — skipping.')
} else {
  pkg.scripts = { ...pkg.scripts, 'dev:vite': DEV_VITE_SCRIPT }
  writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n', 'utf-8')
  console.log('package.json — dev:vite script added.')
}

// --- Create mock/db.json ---
if (existsSync(DB_PATH)) {
  console.log('mock/db.json already exists — skipping.')
} else {
  mkdirSync('mock', { recursive: true })
  writeFileSync(DB_PATH, '{}\n', 'utf-8')
  console.log('mock/db.json created.')
}
