import { getLogger } from '../utils/logger.js';
export class ResearchService {
    insightRepo;
    errorRepo;
    synapseManager;
    logger = getLogger();
    constructor(insightRepo, errorRepo, synapseManager) {
        this.insightRepo = insightRepo;
        this.errorRepo = errorRepo;
        this.synapseManager = synapseManager;
    }
    getInsights(query) {
        if (query.type) {
            return this.insightRepo.findByType(query.type);
        }
        if (query.activeOnly !== false) {
            return this.insightRepo.findActive(query.projectId);
        }
        return this.insightRepo.findActive(query.projectId);
    }
    createInsight(data) {
        const expiresAt = data.expiresInDays
            ? new Date(Date.now() + data.expiresInDays * 86400000).toISOString()
            : null;
        const id = this.insightRepo.create({
            type: data.type,
            title: data.title,
            description: data.description,
            evidence: data.evidence ?? '[]',
            priority: data.priority ?? 0,
            project_id: data.projectId ?? null,
            active: 1,
            expires_at: expiresAt,
        });
        this.logger.info(`Insight created (id=${id}, type=${data.type})`);
        return id;
    }
    getTrends(projectId, windowDays = 7) {
        const now = new Date();
        const currentStart = new Date(now.getTime() - windowDays * 86400000).toISOString();
        const previousStart = new Date(now.getTime() - windowDays * 2 * 86400000).toISOString();
        const currentCount = this.errorRepo.countSince(currentStart, projectId);
        const previousCount = this.errorRepo.countSince(previousStart, projectId) - currentCount;
        const direction = currentCount > previousCount * 1.2
            ? 'increasing'
            : currentCount < previousCount * 0.8
                ? 'decreasing'
                : 'stable';
        return [{
                errorType: 'all',
                count: currentCount,
                direction,
                period: `${windowDays}d`,
            }];
    }
    expireOldInsights() {
        return this.insightRepo.expire();
    }
    rateInsight(id, rating, comment) {
        const clamped = Math.max(-1, Math.min(1, rating)); // -1 (bad), 0 (neutral), 1 (useful)
        return this.insightRepo.rate(id, clamped, comment);
    }
}
//# sourceMappingURL=research.service.js.map