import { randomUUID } from 'node:crypto';
import { promises as fs } from 'node:fs';
import path from 'node:path';
/**
 * Local filesystem attachment storage.
 *
 * Stores files in a configurable directory under the shep home path.
 * The IAttachmentStorage port allows this to be swapped for S3 later.
 */
export class LocalAttachmentStorageService {
    basePath;
    constructor(basePath) {
        this.basePath = basePath;
    }
    async store(input) {
        const id = randomUUID();
        const ext = path.extname(input.filename);
        const storagePath = `${id}${ext}`;
        const fullPath = path.join(this.basePath, storagePath);
        await fs.mkdir(path.dirname(fullPath), { recursive: true });
        await fs.writeFile(fullPath, input.data);
        return storagePath;
    }
    async retrieve(storagePath) {
        const fullPath = path.join(this.basePath, storagePath);
        try {
            return await fs.readFile(fullPath);
        }
        catch {
            return null;
        }
    }
    async delete(storagePath) {
        const fullPath = path.join(this.basePath, storagePath);
        try {
            await fs.unlink(fullPath);
        }
        catch {
            // File may not exist — ignore
        }
    }
    async exists(storagePath) {
        const fullPath = path.join(this.basePath, storagePath);
        try {
            await fs.access(fullPath);
            return true;
        }
        catch {
            return false;
        }
    }
}
