import { GoogleAdsResult, MagicWandResult } from "../types"; export const downloadFile = (filename: string, content: string, contentType: string) => { if (typeof window === 'undefined') return; const blob = new Blob([content], { type: contentType }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.setAttribute("href", url); link.setAttribute("download", filename); document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; export const generateGoogleAdsCSV = (googleAds?: any, magic?: any): string => { const rows: string[][] = []; if (googleAds) { rows.push(["GOOGLE ADS STRATEGY REPORT"]); rows.push(["Generated at", new Date().toLocaleString()]); rows.push([]); const kw = googleAds.keywords; if (kw) { rows.push(["--- KEYWORD RESEARCH ---"]); rows.push(["Type", "Keyword", "CPC"]); if (Array.isArray(kw.primary)) kw.primary.forEach((k: any) => rows.push(["Primary", k.keyword, k.cpc || 'N/A'])); if (Array.isArray(kw.longTail)) kw.longTail.forEach((k: any) => rows.push(["Long-tail", k.keyword, k.cpc || 'N/A'])); if (Array.isArray(kw.negative)) kw.negative.forEach((k: any) => rows.push(["Negative", k.keyword, k.cpc || 'N/A'])); rows.push([]); } const ads = googleAds.adCopies; if (Array.isArray(ads)) { rows.push(["--- AD COPY VARIATIONS ---"]); rows.push(["Variation", "Headlines", "Descriptions", "CTA"]); ads.forEach((ad: any, i: number) => { const hl = Array.isArray(ad.headlines) ? ad.headlines.join(' | ') : ''; const ds = Array.isArray(ad.descriptions) ? ad.descriptions.join(' | ') : ''; rows.push([`Variation ${i + 1}`, hl, ds, ad.callToAction || '']); }); rows.push([]); } const camps = googleAds.campaigns; if (Array.isArray(camps)) { rows.push(["--- CAMPAIGN STRUCTURE ---"]); rows.push(["Name", "Type", "Daily Budget", "Locations", "Schedule"]); camps.forEach((c: any) => { const targeting = c.targeting; const locs = (targeting && Array.isArray(targeting.locations)) ? targeting.locations.join('; ') : 'All'; const sched = (targeting && Array.isArray(targeting.schedule)) ? targeting.schedule.join('; ') : 'All'; rows.push([ c.name || 'Campaign', c.type || 'Search', `${c.budget?.daily || 0} ${c.budget?.currency || 'USD'}`, locs, sched ]); }); rows.push([]); } } if (magic) { rows.push(["--- MARKET INTELLIGENCE ---"]); const ma = magic.marketAnalysis; if (ma) { rows.push(["Growth Rate", ma.growthRate || 'N/A']); const comps = ma.topCompetitors; rows.push(["Top Competitors", Array.isArray(comps) ? comps.join('; ') : 'N/A']); const trends = ma.marketTrends; rows.push(["Market Trends", Array.isArray(trends) ? trends.join('; ') : 'N/A']); } const rationale = magic.rationale; if (rationale) { rows.push(["Strategy Rationale", rationale]); } } return rows.map(row => row.map(cell => `"${String(cell || '').replace(/"/g, '""')}"`).join(",")).join("\n"); }; export const generateGoogleAdsHTML = (googleAds?: any, magic?: any): string => { const parts: string[] = []; parts.push(`Ads Report`); parts.push(`
`); parts.push(`

Google Ads Strategy Report

Generated on ${new Date().toLocaleDateString()}

`); if (googleAds) { parts.push(`

🎯 Keyword Intelligence

`); const kw = googleAds.keywords; if (kw) { const primary = kw.primary; if (Array.isArray(primary)) { parts.push(`
Primary Keywords
`); primary.forEach((k: any) => parts.push(`${k.keyword} (${k.cpc || 'N/A'})`)); parts.push(`
`); } const longTail = kw.longTail; if (Array.isArray(longTail)) { parts.push(`
Long-tail Opportunities
`); longTail.forEach((k: any) => parts.push(`${k.keyword}`)); parts.push(`
`); } } parts.push(`
`); const ads = googleAds.adCopies; if (Array.isArray(ads)) { parts.push(`

✍️ Ad Copy Variations

`); ads.forEach((ad: any, i: number) => { parts.push(`
Variation ${i + 1}
`); const headlines = ad.headlines; if (Array.isArray(headlines)) headlines.forEach(h => parts.push(`
${h}
`)); const descriptions = ad.descriptions; if (Array.isArray(descriptions)) descriptions.forEach(d => parts.push(`
${d}
`)); if (ad.callToAction) parts.push(`
${ad.callToAction}
`); parts.push(`
`); }); parts.push(`
`); } const camps = googleAds.campaigns; if (Array.isArray(camps)) { parts.push(`

🏗️ Campaign Architecture

`); camps.forEach((c: any) => { parts.push(`
${c.name || 'Campaign'}
${c.type || 'Search'}`); parts.push(`
${c.budget?.daily || 0} ${c.budget?.currency || 'USD'}/day
`); }); parts.push(`
`); } } if (magic) { parts.push(`

🧠 Market Intelligence

`); const ma = magic.marketAnalysis; if (ma) { parts.push(`

Market Growth: ${ma.growthRate || 'Stable'}

`); const comps = ma.topCompetitors; if (Array.isArray(comps)) { parts.push(`
Key Competitors
`); comps.forEach(c => parts.push(`
${c}
`)); } } const rationale = magic.rationale; if (rationale) { parts.push(`
Strategy:

${rationale}

`); } parts.push(`
`); } parts.push(`
`); return parts.join(''); };