import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
const txt = readFileSync(resolve(process.cwd(), '.env.local'), 'utf8');
for (const line of txt.split('\n')) { const m=line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/); if(m&&!process.env[m[1]]) process.env[m[1]]=m[2]; }
import { COUNTRY_BY_CODE } from './lib/config';
import { aggregateCountry } from './lib/aggregate';
import { saveCountryAggregate, getCountryView } from './lib/db';
(async () => {
  const fr = COUNTRY_BY_CODE['fr'];
  const agg = await aggregateCountry(fr);
  await saveCountryAggregate('fr', agg, new Date().toISOString());
  const view = await getCountryView('fr');
  console.log('latestAvg=', view.meta?.latestAvg, 'prevAvg=', view.meta?.previousAvg, 'kw=', view.meta?.keywordsTracked, 'locAvg=', view.meta?.locationsAvg);
  console.log('series points=', view.series.length, 'first=', view.series[0], 'last=', view.series[view.series.length-1]);
  console.log('top keywords:');
  view.keywords.slice(0,6).forEach(k => console.log('  ', k.position.toFixed(1), '|', k.keyword, '| loc', k.locationsCount, '| stale', k.stale, '|', k.lastDate));
})().catch(e=>{console.error(e);process.exit(1);});
