import { z } from 'zod';
import { CompanyDatabase } from '../database/db.js';
import { GrowthScorer } from '../analytics/growth_scorer.js';
import { SSBClient } from '../apis/ssb.js';
const AnalyzeGrowthSchema = z.object({
industry: z.string().optional().describe("NACE-kode for bransje"),
region: z.string().optional().describe("Region/fylke"),
min_growth_percent: z.number().default(20).describe("Minimum vekstprosent"),
time_period: z.enum(['1_year', '3_years', '5_years']).default('3_years'),
limit: z.number().default(50).describe("Antall resultater")
});
export async function analyzeGrowth(args: unknown, db: CompanyDatabase, ssb: SSBClient) {
const params = AnalyzeGrowthSchema.parse(args);
const growthScorer = new GrowthScorer();
let companies = await db.getGrowthCompanies(
params.industry,
params.region,
params.min_growth_percent,
params.limit
);
// If no companies with growth data, might be because we don't have financial data
// Suggest using auto-scrape
if (companies.length === 0 && params.industry) {
console.error('💡 No growth companies found - might need to fetch financial data first');
console.error('💡 Tip: Run "Auto-scrape financials" for companies in this industry first');
}
if (companies.length === 0) {
return {
content: [{
type: "text" as const,
text: `Ingen selskaper funnet med minimum ${params.min_growth_percent}% vekst.`
}]
};
}
const formatted = companies.map((c: any, idx: number) => {
const revenueGrowth = c.revenue_growth_percent || 0;
const employeeGrowth = c.employee_growth_percent || 0;
return `${idx + 1}. **${c.name}**
📈 Omsetningsvekst: ${revenueGrowth > 0 ? '+' : ''}${revenueGrowth}%
👥 Ansatt-vekst: ${employeeGrowth > 0 ? '+' : ''}${employeeGrowth}%
💰 Siste omsetning: ${(c.latest_revenue / 1000000).toFixed(1)}M NOK
👔 Ansatte: ${c.latest_employees || 'Ukjent'}
📍 ${c.business_municipality || 'Ukjent'}
🏷️ ${c.nace_description || 'Ukjent bransje'}
📋 Org.nr: ${c.org_nr}`;
}).join('\n\n');
const avgGrowth = companies.reduce((sum: number, c: any) => sum + (c.revenue_growth_percent || 0), 0) / companies.length;
// Fetch SSB growth statistics for comparison
let ssbGrowthStats = null;
try {
ssbGrowthStats = await ssb.getHighGrowthEnterprises(params.industry);
} catch (error) {
console.error('Error fetching SSB growth stats:', error);
}
return {
content: [{
type: "text" as const,
text: `🚀 VEKSTANALYSE
Fant ${companies.length} høyvekstselskaper:
Gjennomsnittlig vekst: ${avgGrowth.toFixed(1)}%
${ssbGrowthStats ? `
📊 SSB REFERANSE:
${ssbGrowthStats.description}
Periode: ${ssbGrowthStats.period}
` : ''}
${formatted}
💡 Bruk 'get_company_details' for å se full profil av et selskap.`
}]
};
}