import { NextResponse } from "next/server"

// 生成最小可用的 OpenAPI 3.1 规范，覆盖当前已实现的关键接口
export async function GET(req: Request) {
  const url = new URL(req.url)
  const download = url.searchParams.get("download") === "1"

  const spec = {
    openapi: "3.1.0",
    info: {
      title: "用户数据资产中台 API",
      version: "1.0.0",
      description:
        "统一用户数据接入与治理接口。包含数据接入、数据库结构浏览等端点。模型结构参考 v1.4 文档中的统一用户画像定义。",
    },
    paths: {
      "/api/ingest": {
        get: {
          summary: "获取数据接入状态",
          responses: {
            "200": {
              description: "成功",
            },
          },
        },
        post: {
          summary: "提交单条数据进行接入处理",
          requestBody: {
            required: true,
            content: {
              "application/json": {
                schema: {
                  type: "object",
                  properties: {
                    source: { type: "string" },
                    sourceUserId: { type: "string" },
                    sourceRecordId: { type: "string" },
                    originalData: { type: "object" },
                    timestamp: { type: "string", format: "date-time" },
                  },
                  required: ["source", "originalData"],
                },
              },
            },
          },
          responses: { "200": { description: "成功" }, "400": { description: "参数错误" } },
        },
        put: {
          summary: "批量接入处理",
          requestBody: {
            required: true,
            content: {
              "application/json": {
                schema: {
                  type: "object",
                  properties: {
                    requests: {
                      type: "array",
                      items: {
                        type: "object",
                        properties: {
                          source: { type: "string" },
                          sourceUserId: { type: "string" },
                          sourceRecordId: { type: "string" },
                          originalData: { type: "object" },
                          timestamp: { type: "string", format: "date-time" },
                        },
                        required: ["source", "originalData"],
                      },
                    },
                  },
                  required: ["requests"],
                },
              },
            },
          },
          responses: { "200": { description: "成功" }, "400": { description: "参数错误" } },
        },
      },
      "/api/database-structure": {
        get: {
          summary: "获取数据库列表或结构",
          parameters: [
            { name: "database", in: "query", required: false, schema: { type: "string" } },
            { name: "table", in: "query", required: false, schema: { type: "string" } },
          ],
          responses: { "200": { description: "成功" } },
        },
      },
    },
  }

  const res = NextResponse.json(spec)
  if (download) {
    res.headers.set("Content-Disposition", 'attachment; filename="openapi.json"')
  }
  return res
}
