/**
 * Test the integration between blog posts and file examples
 * This simulates what happens when a blog post is rendered
 */

import { blogPosts } from "../data/blogPosts";
import { FileExampleManager } from "../data/fileExamples";

/* eslint-disable no-unused-vars */

export async function testBlogPostIntegration() {
  console.log("🧪 Testing Blog Post + File Examples Integration...\n");

  let passed = 0;
  let failed = 0;

  const test = (name: string, fn: () => void | Promise<void>) => {
    try {
      const result = fn();
      if (result instanceof Promise) {
        return result
          .then(() => {
            console.log(`✅ ${name}`);
            passed++;
          })
          .catch((err) => {
            console.log(`❌ ${name}`);
            console.error(`   Error: ${err.message}`);
            failed++;
          });
      } else {
        console.log(`✅ ${name}`);
        passed++;
      }
    } catch (err: any) {
      console.log(`❌ ${name}`);
      console.error(`   Error: ${err.message}`);
      failed++;
    }
  };

  // Test 1: Blog posts exist
  test("Blog posts are loaded", () => {
    if (!blogPosts || blogPosts.length === 0) {
      throw new Error("No blog posts found");
    }
    console.log(`   Found ${blogPosts.length} posts`);
  });

  // Test 2: Each post has required fields
  test("All posts have required fields", () => {
    for (const post of blogPosts) {
      if (!post.slug || !post.title || !post.tags) {
        throw new Error(`Post ${post.slug} missing required fields`);
      }
    }
  });

  // Test 3: Debugging-tips post should have file examples
  test("debugging-tips post has file examples", async () => {
    const post = blogPosts.find((p) => p.slug === "debugging-tips");
    if (!post) {
      throw new Error("debugging-tips post not found");
    }

    // Check if it would trigger file examples
    const _showFileExamples = post.tags?.some((tag) =>
      [
        "architecture",
        "design-patterns",
        "system-design",
        "docker",
        "deployment",
      ].includes(tag),
    );

    // debugging-tips has tags ['debugging', 'tools'] so showFileExamples would be false
    // But it has hardcoded FileExamplesList in the template

    // Verify files exist for this post
    const groups = await FileExampleManager.getAllGroups();
    const filesForPost = groups
      .flatMap((g) => g.files)
      .filter((f) => f.postSlug === "debugging-tips");

    if (filesForPost.length === 0) {
      throw new Error("No files found for debugging-tips");
    }

    console.log(`   Found ${filesForPost.length} files for debugging-tips`);
  });

  // Test 4: Architecture-patterns post should have file examples
  test("architecture-patterns post has file examples", async () => {
    const post = blogPosts.find((p) => p.slug === "architecture-patterns");
    if (!post) {
      throw new Error("architecture-patterns post not found");
    }

    // Check if it would trigger file examples
    const showFileExamples = post.tags?.some((tag) =>
      [
        "architecture",
        "design-patterns",
        "system-design",
        "docker",
        "deployment",
      ].includes(tag),
    );

    if (!showFileExamples) {
      throw new Error("architecture-patterns should show file examples");
    }

    // Verify files exist for this post
    const groups = await FileExampleManager.getAllGroups();
    const filesForPost = groups
      .flatMap((g) => g.files)
      .filter((f) => f.postSlug === "architecture-patterns");

    if (filesForPost.length === 0) {
      throw new Error("No files found for architecture-patterns");
    }

    console.log(
      `   Found ${filesForPost.length} files for architecture-patterns`,
    );
  });

  // Test 5: Docker-deployment post should have file examples
  test("docker-deployment post has file examples", async () => {
    const post = blogPosts.find((p) => p.slug === "docker-deployment");
    if (!post) {
      throw new Error("docker-deployment post not found");
    }

    // Check if it would trigger file examples
    const showFileExamples = post.tags?.some((tag) =>
      [
        "architecture",
        "design-patterns",
        "system-design",
        "docker",
        "deployment",
      ].includes(tag),
    );

    if (!showFileExamples) {
      throw new Error("docker-deployment should show file examples");
    }

    // Verify files exist for this post
    const groups = await FileExampleManager.getAllGroups();
    const filesForPost = groups
      .flatMap((g) => g.files)
      .filter((f) => f.postSlug === "docker-deployment");

    if (filesForPost.length === 0) {
      throw new Error("No files found for docker-deployment");
    }

    console.log(`   Found ${filesForPost.length} files for docker-deployment`);
  });

  // Test 6: First-note post should NOT have file examples
  test("first-note post has no file examples", async () => {
    const post = blogPosts.find((p) => p.slug === "first-note");
    if (!post) {
      throw new Error("first-note post not found");
    }

    // Check if it would trigger file examples
    const showFileExamples = post.tags?.some((tag) =>
      [
        "architecture",
        "design-patterns",
        "system-design",
        "docker",
        "deployment",
      ].includes(tag),
    );

    if (showFileExamples) {
      throw new Error("first-note should NOT show file examples");
    }

    // Verify no files exist for this post
    const groups = await FileExampleManager.getAllGroups();
    const filesForPost = groups
      .flatMap((g) => g.files)
      .filter((f) => f.postSlug === "first-note");

    if (filesForPost.length > 0) {
      throw new Error("Files found for first-note, but none should exist");
    }

    console.log(`   Correctly has no files`);
  });

  // Test 7: Simulate FileExamplesList filtering for debugging-tips
  test("FileExamplesList filtering works for debugging-tips", async () => {
    const postSlug = "debugging-tips";
    const groupId = "python-data-processing";

    const allGroups = await FileExampleManager.getAllGroups();
    const loadedGroups = allGroups
      .filter((g) => g.groupId === groupId)
      .map((g) => ({
        ...g,
        files: g.files.filter((f) => f.postSlug === postSlug),
      }))
      .filter((g) => g.files.length > 0);

    if (loadedGroups.length === 0) {
      throw new Error(
        "No groups loaded for debugging-tips with python-data-processing",
      );
    }

    if (loadedGroups[0].files.length === 0) {
      throw new Error("No files in the group");
    }

    console.log(`   Would show ${loadedGroups[0].files.length} files`);
  });

  // Test 8: Simulate FileExamplesList filtering for architecture-patterns
  test("FileExamplesList filtering works for architecture-patterns", async () => {
    const postSlug = "architecture-patterns";
    const tags = ["architecture", "design-patterns", "system-design"];

    const allGroups = await FileExampleManager.getAllGroups();
    const loadedGroups = allGroups
      .map((g) => ({
        ...g,
        files: g.files.filter((f) => {
          if (f.postSlug !== postSlug) return false;
          if (tags && tags.length > 0) {
            return f.tags?.some((tag) => tags.includes(tag));
          }
          return true;
        }),
      }))
      .filter((g) => g.files.length > 0);

    if (loadedGroups.length === 0) {
      throw new Error("No groups loaded for architecture-patterns");
    }

    const totalFiles = loadedGroups.reduce((sum, g) => sum + g.files.length, 0);
    if (totalFiles === 0) {
      throw new Error("No files found");
    }

    console.log(
      `   Would show ${totalFiles} files across ${loadedGroups.length} groups`,
    );
  });

  // Test 9: Verify all file examples have postSlug
  test("All file examples have postSlug property", async () => {
    const groups = await FileExampleManager.getAllGroups();
    const filesWithoutPostSlug = groups
      .flatMap((g) => g.files)
      .filter((f) => !f.postSlug);

    if (filesWithoutPostSlug.length > 0) {
      throw new Error(`${filesWithoutPostSlug.length} files missing postSlug`);
    }

    console.log(
      `   All ${groups.flatMap((g) => g.files).length} files have postSlug`,
    );
  });

  // Test 10: Verify postSlugs match blog post slugs
  test("File example postSlugs match blog post slugs", async () => {
    const groups = await FileExampleManager.getAllGroups();
    const filePostSlugs = new Set(
      groups.flatMap((g) => g.files).map((f) => f.postSlug),
    );
    const blogPostSlugs = new Set(blogPosts.map((p) => p.slug));

    for (const slug of filePostSlugs) {
      if (slug && !blogPostSlugs.has(slug)) {
        throw new Error(`File postSlug "${slug}" doesn't match any blog post`);
      }
    }

    console.log(`   All file postSlugs match blog posts`);
  });

  // Wait for async tests
  await new Promise((resolve) => setTimeout(resolve, 100));

  console.log(
    `\n📊 Integration Test Results: ${passed} passed, ${failed} failed`,
  );

  if (failed === 0) {
    console.log("🎉 All integration tests passed!");
    console.log(
      "\n✅ The file examples system is correctly integrated with blog posts!",
    );
  } else {
    console.log("❌ Some integration tests failed");
    process.exit(1);
  }
}

// Export for use in other test files
