'use strict';

/**
 * One-shot repair: publish the real body of /blog/oracle-awr-ash-analysis.
 *
 * WHAT WENT WRONG
 * migrations/1749042300000_blog_seed_articles.js seeded this post with
 * stubContent(...) and coming_soon:true — a placeholder reading
 * "[paste awr-ash-guide.html content here]". Someone later flipped it to
 * published without pasting anything, so the live article body has been that
 * placeholder ever since: indexed by Google as a BlogPosting claiming a 14-minute
 * read, listed in sitemap.xml, and linked from LinkedIn.
 *
 * The real 1,835-word article has been in the repo the whole time at
 * data/oracle-docs/awr-ash-guide.html. scripts/seed-blog.js already reads and
 * converts it correctly (line 601) — but that seeder is pre-cutover, Postgres-only,
 * and its own header warns it will not touch Oracle. So the good content was never
 * on a path that reached the live database.
 *
 * This script closes that gap using db/blog.js createPost(), the Oracle-dialect
 * idempotent MERGE that seed-blog.js's header names as the supported route.
 *
 * RUN IT ON THE VM (it needs the live DB env):
 *     node scripts/publish-awr-ash-post.js          # dry run — prints, writes nothing
 *     node scripts/publish-awr-ash-post.js --write  # publishes
 *
 * Idempotent: MERGE on slug, so re-running is safe.
 */

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

const SLUG = 'oracle-awr-ash-analysis';
const SRC = path.join(__dirname, '..', 'data', 'oracle-docs', 'awr-ash-guide.html');

const { htmlToMarkdown } = require('../lib/html-to-markdown');

(async () => {
  const write = process.argv.includes('--write');

  if (!fs.existsSync(SRC)) {
    console.error('FATAL: source not found at ' + SRC);
    process.exit(2);
  }
  const content = htmlToMarkdown(fs.readFileSync(SRC, 'utf8')).trim();

  // Refuse to publish anything that still looks like the placeholder, or that is
  // too short to be the real article — this script exists because a stub reached
  // production once already.
  if (/\[paste\b/i.test(content) || /coming soon/i.test(content)) {
    console.error('FATAL: converted content still contains placeholder text. Aborting.');
    process.exit(2);
  }
  const words = content.split(/\s+/).filter(Boolean).length;
  if (words < 800) {
    console.error('FATAL: converted content is only ' + words + ' words — expected ~1800. Aborting.');
    process.exit(2);
  }

  console.log('slug           : ' + SLUG);
  console.log('source         : ' + SRC);
  console.log('words          : ' + words);
  console.log('first heading  : ' + (content.match(/^## .*/m) || ['(none)'])[0]);
  console.log('');

  if (!write) {
    console.log('DRY RUN — nothing written. Re-run with --write to publish.');
    console.log('--- first 400 chars ---');
    console.log(content.slice(0, 400));
    return;
  }

  const blogDb = require('../db/blog');
  await blogDb.createPost({
    title: 'Oracle AWR and ASH — Reading Reports Like a Senior DBA',
    slug: SLUG,
    excerpt: 'How to read an AWR report without getting lost in the noise: the six sections that '
           + 'matter, what DB Time tells you, how to interpret top wait events, and using ASH to '
           + 'drill into a specific window.',
    content,
    author: 'Kiran Kumar Ale',
    published_at: '2026-04-01',
    read_time_minutes: 14,
  });
  console.log('Published. Verify: https://tunevault.app/blog/' + SLUG);
})().catch((e) => {
  console.error('FAILED:', e.message);
  process.exit(1);
});
