import { NextResponse } from 'next/server';
import { getSql } from '../../../lib/db.js';
import { getOrgId } from '../../../lib/org.js';
import { apiErrorResponse } from '../../../lib/apiErrors.js';
import { updateSecret, deleteSecret } from '../../../lib/repositories/governed-secrets.repository.js';

export const dynamic = 'force-dynamic';
export const revalidate = 0;

export async function PATCH(req, { params }) {
  try {
    const { id } = await params;
    const sql = getSql();
    const orgId = getOrgId(req);

    const body = await req.json().catch(() => ({}));
    const row = await updateSecret(sql, orgId, id, {
      lastRotatedAt: body.last_rotated_at,
      rotationIntervalDays: body.rotation_interval_days,
      notes: body.notes,
    });
    if (!row) return NextResponse.json({ error: 'not_found' }, { status: 404 });
    return NextResponse.json(row);
  } catch (err) {
    return apiErrorResponse(err, 'SECRETS_PATCH');
  }
}

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

    const ok = await deleteSecret(sql, orgId, id);
    if (!ok) return NextResponse.json({ error: 'not_found' }, { status: 404 });
    return NextResponse.json({ deleted: id });
  } catch (err) {
    return apiErrorResponse(err, 'SECRETS_DELETE');
  }
}
