import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
const defaultFs = {
    existsSync,
    readFileSync: (path) => readFileSync(path, 'utf-8'),
    readdirSync: (path) => readdirSync(path),
};
export class ReviewReportFileSystemGateway {
    fs;
    constructor(fs = defaultFs) {
        this.fs = fs;
    }
    buildPath(location) {
        const suffix = suffixFor(location.jobType);
        const fileName = `${location.isoDate}-MR-${location.mergeRequestNumber}-${suffix}.md`;
        return join(location.localPath, '.claude', 'reviews', fileName);
    }
    read(location) {
        const exactPath = this.buildPath(location);
        if (this.fs.existsSync(exactPath)) {
            return { content: this.fs.readFileSync(exactPath), path: exactPath };
        }
        // Fallback: the report file's date prefix may differ from the lookup date
        // (e.g. midnight UTC vs local), so we scan for any *-MR-<N>-<suffix>.md.
        const reviewsDir = join(location.localPath, '.claude', 'reviews');
        if (!this.fs.existsSync(reviewsDir)) {
            return null;
        }
        const pattern = matcherFor(location);
        const match = this.fs.readdirSync(reviewsDir).find(pattern);
        if (!match) {
            return null;
        }
        const resolved = join(reviewsDir, match);
        return { content: this.fs.readFileSync(resolved), path: resolved };
    }
}
function suffixFor(jobType) {
    return jobType === 'followup' ? 'followup' : 'review';
}
function matcherFor(location) {
    const suffix = suffixFor(location.jobType);
    const tail = `-MR-${location.mergeRequestNumber}-${suffix}.md`;
    return (name) => name.endsWith(tail) && /^\d{4}-\d{2}-\d{2}/.test(name);
}
//# sourceMappingURL=reviewReport.fileSystem.gateway.js.map