const { fetchLiveCampaignProducts } = require("./api");
const { fetchLiveCampaignProductsWithHtml } = require("./render");
const { renderDynamicTemplate, renderCampaignsByCount } = require("./templates");
const fs = require("fs");
const path = require("path");

async function test() {
    const data = await fetchLiveCampaignProducts();
    console.log("Campaign Products:", data);
}

async function testRender() {
  const html = await fetchLiveCampaignProductsWithHtml();
  console.log(html); // You can also write it to a file or insert into DOM
}

async function testModernElegantTemplate() {
  try {
    const html = await renderDynamicTemplate("modern-elegant", 12);
    
    const outputPath = path.join(__dirname, "../test-output");
    if (!fs.existsSync(outputPath)) {
      fs.mkdirSync(outputPath, { recursive: true });
    }
    
    fs.writeFileSync(path.join(outputPath, "modern-elegant.html"), html);
    console.log("Modern Elegant template rendered to: test-output/modern-elegant.html");
  } catch (error) {
    console.error("Error testing Modern Elegant template:", error);
  }
}

async function testLuxuryPremiumTemplate() {
  try {
    const html = await renderDynamicTemplate("luxury-premium", 12);
    
    const outputPath = path.join(__dirname, "../test-output");
    if (!fs.existsSync(outputPath)) {
      fs.mkdirSync(outputPath, { recursive: true });
    }
    
    fs.writeFileSync(path.join(outputPath, "luxury-premium.html"), html);
    console.log("Luxury Premium template rendered to: test-output/luxury-premium.html");
  } catch (error) {
    console.error("Error testing Luxury Premium template:", error);
  }
}

async function testCampaignsByCount() {
  try {
    const outputPath = path.join(__dirname, "../test-output");
    if (!fs.existsSync(outputPath)) {
      fs.mkdirSync(outputPath, { recursive: true });
    }

    // Test 1: Get all campaigns
    console.log("Testing campaigns by count - All campaigns");
    const allCampaignsHtml = await renderCampaignsByCount("");
    fs.writeFileSync(path.join(outputPath, "campaigns-all.html"), allCampaignsHtml);
    console.log("All campaigns rendered to: test-output/campaigns-all.html");

    // Test 2: Get first campaign only
    console.log("Testing campaigns by count - First campaign");
    const firstCampaignHtml = await renderCampaignsByCount("1");
    fs.writeFileSync(path.join(outputPath, "campaigns-first.html"), firstCampaignHtml);
    console.log("First campaign rendered to: test-output/campaigns-first.html");

    // Test 3: Get first and second campaigns
    console.log("Testing campaigns by count - First and second campaigns");
    const firstTwoCampaignsHtml = await renderCampaignsByCount("1,2");
    fs.writeFileSync(path.join(outputPath, "campaigns-first-two.html"), firstTwoCampaignsHtml);
    console.log("First two campaigns rendered to: test-output/campaigns-first-two.html");

    // Test 4: Get second and third campaigns
    console.log("Testing campaigns by count - Second and third campaigns");
    const secondThirdCampaignsHtml = await renderCampaignsByCount("2,3");
    fs.writeFileSync(path.join(outputPath, "campaigns-second-third.html"), secondThirdCampaignsHtml);
    console.log("Second and third campaigns rendered to: test-output/campaigns-second-third.html");

  } catch (error) {
    console.error("Error testing campaigns by count:", error);
  }
}

// Test all templates and functions
testModernElegantTemplate();
testLuxuryPremiumTemplate();
testCampaignsByCount();

// Uncomment these if needed
// test();
// testRender();
