"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.browseFiles = browseFiles;
exports.getFileMetadata = getFileMetadata;
exports.deleteFile = deleteFile;
exports.uploadFile = uploadFile;
exports.generateSignedUrl = generateSignedUrl;
const storage_1 = require("../lib/storage");
const mime_1 = require("../lib/storage/mime");
const publish_1 = require("../lib/events/publish");
const ALLOWED_EXPIRY = [3600, 21600, 86400, 604800, 2592000]; // 1h, 6h, 24h, 7d, 30d
async function browseFiles(input) {
    try {
        const result = await (0, storage_1.getStorageBackend)().listWithPrefixes(input.prefix, input.pageSize || 100, input.continuationToken);
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
async function getFileMetadata(input) {
    try {
        const metadata = await (0, storage_1.getStorageBackend)().getMetadata(input.filePath);
        const contentType = (!metadata.content_type || metadata.content_type === 'application/octet-stream')
            ? (0, mime_1.mimeFromPath)(input.filePath)
            : metadata.content_type;
        return { status: 200, data: { path: input.filePath, ...metadata, content_type: contentType } };
    }
    catch (err) {
        if (err.message?.includes('not found') || err.name === 'NotFound') {
            return { status: 404, error: 'File not found' };
        }
        return { status: 500, error: err.message };
    }
}
async function deleteFile(input) {
    try {
        const result = await (0, storage_1.getStorageBackend)().delete(input.filePath);
        if (!result.deleted) {
            return { status: 404, error: 'File not found' };
        }
        (0, publish_1.publishFileEvent)({ type: 'file.deleted', path: input.filePath });
        return { status: 200, data: { deleted: true, path: input.filePath } };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
async function uploadFile(input) {
    try {
        const result = await (0, storage_1.getStorageBackend)().write(input.path, input.buffer);
        const contentType = (0, mime_1.mimeFromPath)(input.path);
        (0, publish_1.publishFileEvent)({
            type: 'file.stored',
            path: input.path,
            size: result.size,
            mime: contentType,
        });
        return {
            status: 200,
            data: { path: input.path, size: result.size, content_type: contentType },
        };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
async function generateSignedUrl(input) {
    if (!ALLOWED_EXPIRY.includes(input.expiresIn)) {
        return {
            status: 400,
            error: `expiresIn must be one of: ${ALLOWED_EXPIRY.join(', ')} (seconds)`,
        };
    }
    try {
        const url = await (0, storage_1.getStorageBackend)().getSignedUrl(input.filePath, input.expiresIn);
        const expiresAt = new Date(Date.now() + input.expiresIn * 1000).toISOString();
        return { status: 200, data: { url, expiresAt } };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
