const fs = require('fs');
let code = fs.readFileSync('seo-server.js', 'utf8');

// Fix idFromSlug to extract last 6 chars of slug
code = code.replace(
  `function idFromSlug(slug) {
  // last segment after final dash is the 6-char id prefix
  const parts = slug.split('-');
  return parts[parts.length - 1];
}`,
  `function idFromSlug(slug) {
  // UUID first segment is 8 chars, slug ends with first 6
  const parts = slug.split('-');
  const last = parts[parts.length - 1];
  // Try last part first, then last 6 chars of whole slug
  return last.length >= 6 ? last : slug.slice(-6);
}`
);

// Fix the DB query to use ilike on id properly
code = code.replace(
  `.ilike('id', \`\${prefix}%\`)`,
  `.ilike('id', \`\${prefix}%\`).in('status', ['open', 'active', 'expired', 'closed'])`
);

fs.writeFileSync('seo-server.js', code);
console.log('Fixed!');
