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(["Website", googleAds.websiteUrl || 'N/A']); 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", String(k?.keyword || ''), String(k?.cpc || '')])); if (Array.isArray(kw.longTail)) kw.longTail.forEach((k: any) => rows.push(["Long-tail", String(k?.keyword || ''), String(k?.cpc || '')])); if (Array.isArray(kw.negative)) kw.negative.forEach((k: any) => rows.push(["Negative", String(k?.keyword || ''), String(k?.cpc || '')])); rows.push([]); } const ads = googleAds.adCopies; if (Array.isArray(ads)) { rows.push(["AD COPIES"]); 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([`Var ${i + 1}`, hl, ds, String(ad?.callToAction || '')]); }); rows.push([]); } const camps = googleAds.campaigns; if (Array.isArray(camps)) { rows.push(["CAMPAIGN STRUCTURE"]); rows.push(["Name", "Type", "Budget", "Locations", "Schedule"]); camps.forEach((c: any) => { const t = c.targeting; const locs = (t && Array.isArray(t.locations)) ? t.locations.join('; ') : 'All'; const sched = (t && Array.isArray(t.schedule)) ? t.schedule.join('; ') : 'All'; rows.push([String(c.name || ''), String(c.type || ''), `${c?.budget?.daily || 0} ${c?.budget?.currency || ''}`, locs, sched]); }); rows.push([]); } const impl = googleAds.implementation; if (impl) { rows.push(["IMPLEMENTATION"]); if (Array.isArray(impl.setupSteps)) impl.setupSteps.forEach((s: any) => rows.push(["Setup", String(s)])); if (Array.isArray(impl.qualityScoreTips)) impl.qualityScoreTips.forEach((s: any) => rows.push(["QS Tip", String(s)])); rows.push([]); } } if (magic) { rows.push(["MARKET INTELLIGENCE"]); const ma = magic.marketAnalysis; if (ma) { rows.push(["Size", String(ma.industrySize || '')]); rows.push(["Growth", String(ma.growthRate || '')]); rows.push(["Trends", Array.isArray(ma.marketTrends) ? ma.marketTrends.join('; ') : '']); rows.push(["Competitors", Array.isArray(ma.topCompetitors) ? ma.topCompetitors.join('; ') : '']); rows.push([]); } const strats = magic.strategies; if (Array.isArray(strats)) { rows.push(["STRATEGIES"]); strats.forEach((s: any) => { rows.push(["Direction", String(s.direction || '')]); rows.push(["Target", String(s.targetAudience || '')]); rows.push(["Rationale", String(s.rationale || '')]); rows.push(["ROI", String(s.expectedROI || '')]); rows.push([]); }); } } return rows.map(r => r.map(c => `"${String(c || '').replace(/"/g, '""')}"`).join(",")).join("\n"); }; export const generateGoogleAdsHTML = (googleAds?: any, magic?: any): string => { const parts: string[] = []; parts.push(`Report`); parts.push(`

Marketing Strategy

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

Keywords

`); const kw = googleAds.keywords; if (kw && Array.isArray(kw.primary)) { parts.push(`

Primary

`); kw.primary.forEach((k: any) => parts.push(`${k.keyword} `)); } parts.push(`
`); const ads = googleAds.adCopies; if (Array.isArray(ads)) { parts.push(`

Ads

`); ads.forEach((ad: any) => { parts.push(`
${(ad.headlines || [])[0] || ''}

${(ad.descriptions || [])[0] || ''}

`); }); parts.push(`
`); } } if (magic) { parts.push(`

Market

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

Size: ${ma.industrySize || 'N/A'}

`); parts.push(`

Growth: ${ma.growthRate || 'N/A'}

`); } const strats = magic.strategies; if (Array.isArray(strats)) { parts.push(`

Strategies

`); strats.forEach((s: any) => { parts.push(`

${s.direction}

${s.rationale}

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