docker run --rm -it \
  -v /share/Container/dms/frontend:/app \
  -w /app \
  -e CI=1 \
  node:20-alpine sh -lc '
set -eu

echo "1) ตรวจและแก้ package.json → next/react/react-dom ต้องอยู่ใน dependencies + scripts ครบ"
test -f package.json || { echo "❌ ไม่พบ package.json ที่ /app"; exit 1; }
node -e "
const fs=require(\"fs\");
const p=JSON.parse(fs.readFileSync(\"package.json\",\"utf8\"));
p.dependencies=p.dependencies||{};
p.devDependencies=p.devDependencies||{};
p.scripts=p.scripts||{};
let changed=false;

for(const k of [\"next\",\"react\",\"react-dom\"]){
  if(p.devDependencies[k]){ p.dependencies[k]=p.devDependencies[k]; delete p.devDependencies[k]; changed=true; }
  if(!p.dependencies[k]){ p.dependencies[k]=\"latest\"; changed=true; }
}
if(!p.scripts.dev){ p.scripts.dev=\"next dev\"; changed=true; }
if(!p.scripts.build){ p.scripts.build=\"next build\"; changed=true; }
if(!p.scripts.start){ p.scripts.start=\"next start\"; changed=true; }

if(changed){ fs.writeFileSync(\"package.json\",JSON.stringify(p,null,2)); console.log(\"package.json patched\"); }
"
npm i

echo "2) โครง Next.js: app/ หรือ src/app/ + layout.jsx + globals.css"
APPDIR="app"; [ -d src/app ] && APPDIR="src/app"
mkdir -p "$APPDIR"
[ -f "$APPDIR/globals.css" ] || printf "%s\n%s\n%s\n" "@tailwind base;" "@tailwind components;" "@tailwind utilities;" > "$APPDIR/globals.css"
if [ ! -f "$APPDIR/layout.jsx" ] && [ ! -f "$APPDIR/layout.tsx" ]; then
  cat > "$APPDIR/layout.jsx" <<EOF
import "./globals.css";
export default function RootLayout({ children }) {
  return (<html lang="th"><body>{children}</body></html>);
}
EOF
fi
grep -q "import \"./globals.css\"" "$APPDIR/layout.jsx" 2>/dev/null || sed -i "1i import \"./globals.css\";" "$APPDIR/layout.jsx" 2>/dev/null || true
[ -f "$APPDIR/page.jsx" ] || [ -f "$APPDIR/page.tsx" ] || echo '\''export default function Page(){return <main className="p-6">OK</main>}'\'' > "$APPDIR/page.jsx"

echo "3) สร้าง/อัปเดตไฟล์ config ที่ CLI ต้องมี: jsconfig, postcss, tailwind, next.config"
[ -f jsconfig.json ] || cat > jsconfig.json <<JSON
{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }
JSON

[ -f postcss.config.cjs ] || echo "module.exports={plugins:{tailwindcss:{},autoprefixer:{}}}" > postcss.config.cjs

[ -f tailwind.config.js ] || npx tailwindcss init -p
grep -q "content:" tailwind.config.js || \
  sed -i "s|module.exports = {|module.exports = {\n  content: [\"./$APPDIR/**/*.{js,jsx,ts,tsx,mdx}\", \"./components/**/*.{js,jsx,ts,tsx,mdx}\", \"./pages/**/*.{js,jsx,ts,tsx,mdx}\", \"./src/**/*.{js,jsx,ts,tsx,mdx}\"],|g" tailwind.config.js
grep -q "tailwindcss-animate" tailwind.config.js || \
  sed -i '\''s|plugins: \[|plugins: [require("tailwindcss-animate"), |; s|plugins: \[\]|plugins: [require("tailwindcss-animate")]|'\'' tailwind.config.js

# next.config (บางเวอร์ชัน CLI เช็คไฟล์นี้ด้วย)
if [ ! -f next.config.js ] && [ ! -f next.config.mjs ]; then
  cat > next.config.js <<EOF
/** @type {import('next').NextConfig} */
const nextConfig = { reactStrictMode: true };
module.exports = nextConfig;
EOF
fi

echo "4) components.json (คงของเดิม ถ้าไม่มีค่อยสร้าง)"
if [ ! -f components.json ]; then
  TYPESCRIPT=false; [ -f tsconfig.json ] && TYPESCRIPT=true
  TAILWIND_FILE="tailwind.config.js"; [ -f tailwind.config.ts ] && TAILWIND_FILE="tailwind.config.ts"
  cat > components.json <<EOF
{
  "\$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "rsc": true,
  "tsx": $TYPESCRIPT,
  "tailwind": {
    "config": "$TAILWIND_FILE",
    "css": "$APPDIR/globals.css",
    "baseColor": "slate",
    "cssVariables": true
  },
  "aliases": { "components": "@/components", "utils": "@/lib/utils" }
}
EOF
fi

echo "5) ติดตั้ง dev deps tailwind/postcss (idempotent)"
npm i -D tailwindcss postcss autoprefixer tailwindcss-animate >/dev/null 2>&1 || true

echo "6) init (force) — ตอนนี้ควรผ่านการตรวจจับแล้ว"
npx shadcn@latest init -y -f --no-src-dir

echo "✅ เสร็จ — ถ้าต้องการเพิ่มคอมโพเนนต์ต่อ:"
echo "npx shadcn@latest add -y dialog alert-dialog dropdown-menu checkbox scroll-area tabs tooltip switch button label input card badge progress tabs"
'
