import { NextResponse } from "next/server";
import { HeadBucketCommand } from "@aws-sdk/client-s3";
import { s3, BUCKET } from "@/lib/storage";

export async function GET() {
  const results: Record<string, unknown> = {
    storage: { status: "unknown" },
    storageConfig: {
      endpoint: process.env.STORAGE_ENDPOINT || "(not set)",
      bucket: BUCKET,
    },
  };

  try {
    await s3.send(new HeadBucketCommand({ Bucket: BUCKET }));
    results.storage = { status: "ok", bucket: BUCKET };
  } catch (error: unknown) {
    const err = error as Error & { $metadata?: unknown; Code?: string };
    results.storage = {
      status: "error",
      message: err.message,
      code: err.Code,
      metadata: err.$metadata,
    };
  }

  return NextResponse.json(results, { status: 200 });
}
