import fs from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";
import { piDir } from "../pi-dir.js";
const STORE_DIR = piDir();
const STORE_FILE = path.join(STORE_DIR, "gateway-goal-artifacts.json");
export class GoalArtifactStore {
    artifacts = new Map();
    constructor() {
        this.load();
    }
    load() {
        try {
            if (fs.existsSync(STORE_FILE)) {
                const data = JSON.parse(fs.readFileSync(STORE_FILE, "utf-8"));
                if (Array.isArray(data)) {
                    for (const a of data) {
                        if (a.id) {
                            // Migration: existing artifacts without status are accepted
                            if (a.status === undefined) {
                                a.status = "accepted";
                            }
                            // Migration: map legacy specId to workflowArtifactId
                            if (a.specId && !a.workflowArtifactId) {
                                a.workflowArtifactId = a.specId;
                            }
                            this.artifacts.set(a.id, a);
                        }
                    }
                }
            }
        }
        catch (err) {
            console.error("[goal-artifact-store] Failed to load persisted artifacts:", err);
        }
    }
    save() {
        try {
            if (!fs.existsSync(STORE_DIR)) {
                fs.mkdirSync(STORE_DIR, { recursive: true });
            }
            const data = Array.from(this.artifacts.values());
            fs.writeFileSync(STORE_FILE, JSON.stringify(data, null, 2), "utf-8");
        }
        catch (err) {
            console.error("[goal-artifact-store] Failed to save artifacts:", err);
        }
    }
    create(artifact) {
        const now = Date.now();
        const full = {
            ...artifact,
            status: artifact.status ?? "submitted",
            id: randomUUID(),
            version: 1,
            createdAt: now,
            updatedAt: now,
        };
        this.artifacts.set(full.id, full);
        this.save();
        return full;
    }
    get(id) {
        return this.artifacts.get(id);
    }
    getByGoalId(goalId) {
        return Array.from(this.artifacts.values()).filter((a) => a.goalId === goalId);
    }
    update(id, updates) {
        const existing = this.artifacts.get(id);
        if (!existing)
            return undefined;
        const cleaned = {};
        for (const [k, v] of Object.entries(updates)) {
            if (v !== undefined)
                cleaned[k] = v;
        }
        Object.assign(existing, cleaned, {
            version: existing.version + 1,
            updatedAt: Date.now(),
        });
        this.save();
        return existing;
    }
    delete(id) {
        const existed = this.artifacts.has(id);
        if (existed) {
            this.artifacts.delete(id);
            this.save();
        }
        return existed;
    }
}
//# sourceMappingURL=goal-artifact-store.js.map