import { d as defineEventHandler, b as getRouterParam, c as createError, u as useRuntimeConfig } from '../../../../../nitro/nitro.mjs';
import { unlink } from 'fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
import { a as findById, r as remove } from '../../../../../_/attachments.service.mjs';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'stream';
import 'events';
import 'http';
import 'crypto';
import 'buffer';
import 'zlib';
import 'https';
import 'net';
import 'tls';
import 'url';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'vue-router';
import '@prisma/client';
import '@prisma/adapter-better-sqlite3';
import 'child_process';
import 'node:child_process';
import 'node:url';

function getAutocodeRoot() {
  const config = useRuntimeConfig();
  return config["autocodeRoot"] || join(process.cwd(), "autocode");
}
const _attachmentId__delete = defineEventHandler(async (event) => {
  const issueId = getRouterParam(event, "id");
  const attachmentIdParam = getRouterParam(event, "attachmentId");
  if (!issueId || !attachmentIdParam) {
    throw createError({
      statusCode: 400,
      message: "Issue ID and Attachment ID required"
    });
  }
  const attachmentId = parseInt(attachmentIdParam, 10);
  if (isNaN(attachmentId)) {
    throw createError({
      statusCode: 400,
      message: "Invalid attachment ID"
    });
  }
  const attachment = await findById(attachmentId);
  if (!attachment || attachment.issueKey !== issueId) {
    throw createError({
      statusCode: 404,
      message: "Attachment not found"
    });
  }
  const autocodeRoot = getAutocodeRoot();
  const filepath = join(autocodeRoot, attachment.filepath);
  if (existsSync(filepath)) {
    try {
      await unlink(filepath);
    } catch (err) {
      console.error(`Failed to delete attachment file: ${filepath}`, err);
    }
  }
  await remove(attachmentId);
  return { success: true };
});

export { _attachmentId__delete as default };
//# sourceMappingURL=_attachmentId_.delete.mjs.map
