const fs = require('fs');
const path = require('path');

const files = [
  "risk/risk-pools-console.tsx",
  "risk/risk-index-console.tsx",
  "admin-roles/admin-roles-console.tsx",
  "admin-users/admin-users-console.tsx",
  "wallet/wallet-console.tsx",
  "integration/integration-sites-console.tsx",
  "players/players-console.tsx",
  "config/doc/risk-cap-doc-screen.tsx",
  "draws/draw-review-console.tsx",
  "draws/draws-index-console.tsx",
  "tickets/player-tickets-console.tsx",
  "settings/currency-settings-panel.tsx",
  "agents/agents-console.tsx",
  "settlement/settlement-batches-console.tsx"
];

const STICKY_HEAD_CLASSES = "sticky right-0 z-20 bg-muted shadow-[-1px_0_0_rgba(203,213,225,0.7)] ";
const STICKY_CELL_CLASSES = "sticky right-0 z-10 bg-card shadow-[-1px_0_0_rgba(203,213,225,0.7)] ";

files.forEach(file => {
  const fullPath = path.join("/Users/kang/Work/lotterySystem/lotteryadmin/src/modules", file);
  if (!fs.existsSync(fullPath)) {
    console.log("Not found:", file);
    return;
  }
  
  let content = fs.readFileSync(fullPath, 'utf8');
  let changed = false;
  
  // Replace TableHead
  const newContent1 = content.replace(
    /<TableHead className="([^"]*text-center[^"]*)"([^>]*)>(.*?t\([^)]*(?:action|操作)[^)]*\).*?)<\/TableHead>/g,
    (match, p1, p2, p3) => {
      if (p1.includes("sticky")) return match;
      changed = true;
      return `<TableHead className="${STICKY_HEAD_CLASSES}${p1}"${p2}>${p3}</TableHead>`;
    }
  );
  content = newContent1;
  
  // Replace TableCell wrapping AdminRowActionsMenu
  const newContent2 = content.replace(
    /<TableCell([^>]*)>\s*<AdminRowActionsMenu/g,
    (match, p1) => {
      if (match.includes("sticky")) return match;
      changed = true;
      if (p1.includes('className="')) {
        return match.replace(/className="([^"]*)"/, `className="${STICKY_CELL_CLASSES}$1"`);
      } else {
        return `<TableCell className="${STICKY_CELL_CLASSES}"${p1}>\n<AdminRowActionsMenu`;
      }
    }
  );
  content = newContent2;

  if (changed) {
    fs.writeFileSync(fullPath, content, 'utf8');
    console.log("Updated", file);
  } else {
    console.log("Skipped", file);
  }
});
