import { appendFile, mkdir } from "node:fs/promises";
import { dirname, resolve } from "node:path";

export type ExtensionToggleAuditEntry = {
    timestamp: string;
    streamId: string;
    extensionId: string;
    enabled: boolean;
    tokenId: string;
    sessionId: string | null;
    result: "success" | "failed";
    error?: string;
};

export class ExtensionAuditLog {
    public constructor(private readonly filePath: string) {}

    public async appendToggle(entry: ExtensionToggleAuditEntry): Promise<void> {
        const resolvedPath = resolve(process.cwd(), this.filePath);
        await mkdir(dirname(resolvedPath), { recursive: true });
        await appendFile(resolvedPath, `${JSON.stringify(entry)}\n`, "utf8");
    }
}
