import define from "../../define.js";
import { VerifiedBadgeRequests, Users } from "../../../../models/index.js";
import { genId } from "../../../../misc/gen-id.js";
export const meta = {
    tags: [
        "account"
    ],
    requireCredential: true,
    kind: "write:account"
};
export const paramDef = {
    type: "object",
    properties: {
        comment: {
            type: "string",
            maxLength: 2048,
            default: ""
        }
    },
    required: []
};
export default define(meta, paramDef, async (ps, user)=>{
    const me = await Users.findOneByOrFail({
        id: user.id
    });
    if (me.isVerified) {
        throw new Error("already verified");
    }
    const existing = await VerifiedBadgeRequests.findOneBy({
        userId: user.id,
        status: "pending"
    });
    if (existing != null) {
        return await VerifiedBadgeRequests.pack(existing);
    }
    const request = await VerifiedBadgeRequests.insert({
        id: genId(),
        createdAt: new Date(),
        resolvedAt: null,
        userId: user.id,
        resolverId: null,
        status: "pending",
        comment: ps.comment ?? ""
    }).then((x)=>VerifiedBadgeRequests.findOneByOrFail(x.identifiers[0]));
    return await VerifiedBadgeRequests.pack(request);
});
