---
import Base from '../../layouts/Base.astro';
import fs from 'node:fs';
import path from 'node:path';

export function getStaticPaths() {
  function getAllHtmlFiles(dir) {
    let results = [];
    if (!fs.existsSync(dir)) return results;
    const entries = fs.readdirSync(dir, { withFileTypes: true });
    for (const entry of entries) {
      const fullPath = path.join(dir, entry.name);
      if (entry.isDirectory()) {
        results = results.concat(getAllHtmlFiles(fullPath));
      } else if (entry.name.endsWith('.html')) {
        results.push(fullPath);
      }
    }
    return results;
  }

  const dataDir = path.join(process.cwd(), 'src/data/blog');
  const files = getAllHtmlFiles(dataDir);
  
  return files.map(file => {
    const relative = file.replace(dataDir + '/', '');
    
    let slug;
    
    if (relative.includes('?')) {
      // URL-encode the ? to %3F for the slug
      slug = relative.replace('.html', '').replace(/\?/g, '%3F');
    } else if (relative.endsWith('/index.html') || relative === 'index.html') {
      slug = relative.replace(/\/?index\.html$/, '');
    } else {
      slug = relative.replace('.html', '');
    }
    
    return {
      params: { slug: slug || undefined },
      props: { filePath: file }
    };
  });
}

const { filePath } = Astro.props;
const html = fs.readFileSync(filePath, 'utf-8');

// Extract title
const titleMatch = html.match(/<title>([^<]+)<\/title>/i);
const title = titleMatch ? titleMatch[1].replace(/&amp;/g, '&').replace(/&#039;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&laquo;/g, '«').replace(/&raquo;/g, '»') : 'Blog';

// Extract meta description
const descMatch = html.match(/<meta\s+name="description"\s+content="([^"]*)"/i);
const description = descMatch ? descMatch[1] : '';

// Extract content - try to get the main content area
let content = html;

// Try to extract just the body content
const bodyStart = content.indexOf('<body');
if (bodyStart > 0) {
  const bodyTagEnd = content.indexOf('>', bodyStart);
  content = content.substring(bodyTagEnd + 1);
}
const bodyEnd = content.indexOf('</body>');
if (bodyEnd > 0) {
  content = content.substring(0, bodyEnd);
}

// Remove the d-page wrapper's header (navigation) - we use Base.astro's header
const headerEnd = content.indexOf('</div><!-- d-header -->');
if (headerEnd > 0) {
  content = content.substring(headerEnd + 22);
}

// Try to extract d-content-wrap or d-main
const contentWrapStart = content.indexOf('class="d-content-wrap"');
if (contentWrapStart > 0) {
  const tagStart = content.lastIndexOf('<div', contentWrapStart);
  content = content.substring(tagStart);
  const footerStart = content.indexOf('<div class="d-footer">');
  if (footerStart > 0) content = content.substring(0, footerStart);
  const pageEnd = content.lastIndexOf('</div><!-- d-page -->');
  if (pageEnd > 0) content = content.substring(0, pageEnd);
}

// Remove any remaining script tags that reference old WordPress paths
content = content.replace(/<script[^>]*src="[^"]*wp-includes[^"]*"[^>]*><\/script>/gi, '');
content = content.replace(/<script[^>]*src="[^"]*wp-content[^"]*"[^>]*><\/script>/gi, '');
content = content.replace(/<script[^>]*src="[^"]*wp-embed[^"]*"[^>]*><\/script>/gi, '');
---
<Base title={title} description={description}>
  <Fragment set:html={content} />
</Base>
