export const dynamic = 'force-dynamic';

import { NextResponse } from 'next/server';
import { getSql } from '../../../lib/db.js';
import { getOrgId } from '../../../lib/org.js';
import { apiErrorResponse } from '../../../lib/apiErrors.js';
import { getArtifact, deleteArtifact } from '../../../lib/repositories/artifacts.repository.js';

export async function GET(request, { params }) {
  try {
    const { artifactId } = await params;
    const sql = getSql();
    const orgId = getOrgId(request);

    const artifact = await getArtifact(sql, orgId, artifactId);
    if (!artifact) {
      return NextResponse.json({ error: 'artifact_not_found' }, { status: 404 });
    }

    return NextResponse.json(artifact);
  } catch (error) {
    return apiErrorResponse(error, 'ARTIFACT_DETAIL');
  }
}

export async function DELETE(request, { params }) {
  try {
    const { artifactId } = await params;
    const sql = getSql();
    const orgId = getOrgId(request);

    const result = await deleteArtifact(sql, orgId, artifactId);
    if (!result) {
      return NextResponse.json({ error: 'artifact_not_found' }, { status: 404 });
    }

    return NextResponse.json({ deleted: true });
  } catch (error) {
    return apiErrorResponse(error, 'ARTIFACT_DELETE');
  }
}
