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

const filePath = path.join(__dirname, '../src/lib/blog-data.ts');
let content = fs.readFileSync(filePath, 'utf-8');

// Function to format date from ISO format
function formatDate(isoDate) {
  const date = new Date(isoDate + 'T00:00:00Z');
  const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  return `${months[date.getUTCMonth()]} ${date.getUTCDate()}, ${date.getUTCFullYear()}`;
}

// Replace each post's content to add metadata div
content = content.replace(
  /content:\s*`<div class="blog-content">/g,
  (match) => {
    // We'll do a more sophisticated replacement with the post data
    return match;
  }
);

// Actually, we need a smarter approach - match each post and extract date info
// Let's use a different strategy: find each post object and inject the metadata

const postRegex = /(\{\s*slug:\s*"([^"]+)"[\s\S]*?publishDate:\s*"([^"]+)"[\s\S]*?dateModified:\s*"([^"]+)"[\s\S]*?authorName:\s*"([^"]+)"[\s\S]*?authorTitle:\s*"([^"]+)"[\s\S]*?content:\s*`<div class="blog-content">)/g;

let match;
const replacements = [];

while ((match = postRegex.exec(content)) !== null) {
  const fullMatch = match[0];
  const slug = match[2];
  const publishDate = match[3];
  const dateModified = match[4];
  const authorName = match[5];
  const authorTitle = match[6];

  const publishFormatted = formatDate(publishDate);
  const modifiedFormatted = formatDate(dateModified);

  const metadataDiv = `<div class="post-metadata bg-blue-50 p-4 rounded-lg mb-8 border-l-4 border-blue-500">
        <p class="text-sm text-gray-700">
          <strong>Author:</strong> ${authorName}, ${authorTitle}<br/>
          📅 <strong>Published:</strong> ${publishFormatted} | <strong>Last updated:</strong> ${modifiedFormatted}
        </p>
      </div>
      `;

  const replacement = fullMatch.replace(
    '<div class="blog-content">',
    `<div class="blog-content">
      ${metadataDiv}`
  );

  replacements.push({ original: fullMatch, replacement, slug });
}

// Apply replacements in reverse order to maintain indices
replacements.reverse().forEach(({ original, replacement }) => {
  content = content.replace(original, replacement);
});

fs.writeFileSync(filePath, content, 'utf-8');
console.log(`✅ Added metadata divs to ${replacements.length} posts`);
replacements.forEach(r => console.log(`   - ${r.slug}`));
