import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { createEmptyStats } from '../../../../../modules/tracking/entities/tracking/mrTrackingData.js';
function getTrackingPath(projectPath) {
    return join(projectPath, '.claude', 'reviews', 'mr-tracking.json');
}
function createEmptyTrackingData() {
    return {
        mrs: [],
        lastUpdated: new Date().toISOString(),
        stats: createEmptyStats(),
    };
}
export class FileSystemReviewRequestTrackingGateway {
    statsCalculator;
    constructor(statsCalculator) {
        this.statsCalculator = statsCalculator;
    }
    loadTracking(projectPath) {
        const trackingPath = getTrackingPath(projectPath);
        if (!existsSync(trackingPath)) {
            return null;
        }
        try {
            const content = readFileSync(trackingPath, 'utf-8');
            const data = JSON.parse(content);
            if (!Array.isArray(data.mrs)) {
                data.mrs = [];
            }
            if (!data.stats) {
                data.stats = createEmptyStats();
            }
            return data;
        }
        catch {
            return null;
        }
    }
    saveTracking(projectPath, data) {
        const trackingPath = getTrackingPath(projectPath);
        const dir = dirname(trackingPath);
        if (!existsSync(dir)) {
            mkdirSync(dir, { recursive: true });
        }
        data.stats = this.statsCalculator.compute(data.mrs);
        data.lastUpdated = new Date().toISOString();
        writeFileSync(trackingPath, JSON.stringify(data, null, 2), 'utf-8');
    }
    getById(projectPath, reviewRequestId) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return null;
        return data.mrs.find((mr) => mr.id === reviewRequestId) ?? null;
    }
    getByNumber(projectPath, reviewRequestNumber, platform) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return null;
        return data.mrs.find((mr) => mr.mrNumber === reviewRequestNumber && mr.platform === platform) ?? null;
    }
    create(projectPath, reviewRequest) {
        let data = this.loadTracking(projectPath);
        if (!data) {
            data = createEmptyTrackingData();
        }
        data.mrs.push(reviewRequest);
        data.stats.totalMrs = data.mrs.length;
        this.saveTracking(projectPath, data);
    }
    update(projectPath, reviewRequestId, updates) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return;
        const index = data.mrs.findIndex((mr) => mr.id === reviewRequestId);
        if (index === -1)
            return;
        data.mrs[index] = { ...data.mrs[index], ...updates };
        this.saveTracking(projectPath, data);
    }
    getByState(projectPath, state) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return [];
        return data.mrs.filter((mr) => mr.state === state);
    }
    getActiveMrs(projectPath) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return [];
        return data.mrs.filter((mr) => mr.state !== 'merged' && mr.state !== 'closed');
    }
    remove(projectPath, reviewRequestId) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return false;
        const index = data.mrs.findIndex((mr) => mr.id === reviewRequestId);
        if (index === -1)
            return false;
        data.mrs.splice(index, 1);
        data.stats.totalMrs = data.mrs.length;
        this.saveTracking(projectPath, data);
        return true;
    }
    archive(projectPath, reviewRequestId) {
        return this.remove(projectPath, reviewRequestId);
    }
    recordReviewEvent(projectPath, reviewRequestId, event) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return;
        const mr = data.mrs.find((m) => m.id === reviewRequestId);
        if (!mr)
            return;
        mr.reviews.push(event);
        mr.lastReviewAt = event.timestamp;
        if (event.type === 'review') {
            mr.totalReviews++;
            data.stats.totalReviews++;
        }
        else {
            mr.totalFollowups++;
            data.stats.totalFollowups++;
        }
        mr.totalBlocking += event.blocking;
        mr.totalWarnings += event.warnings;
        mr.totalSuggestions += event.suggestions;
        mr.totalDurationMs += event.durationMs;
        this.saveTracking(projectPath, data);
    }
    recordPush(projectPath, reviewRequestNumber, platform) {
        const data = this.loadTracking(projectPath);
        if (!data)
            return null;
        const mr = data.mrs.find((m) => m.mrNumber === reviewRequestNumber && m.platform === platform);
        if (!mr)
            return null;
        mr.lastPushAt = new Date().toISOString();
        this.saveTracking(projectPath, data);
        return mr;
    }
}
//# sourceMappingURL=reviewRequestTracking.fileSystem.js.map