import define from "../../../define.js";
import { Plans } from "../../../../../models/index.js";
import { genId } from "../../../../../misc/gen-id.js";
import { insertModerationLog } from "../../../../../services/insert-moderation-log.js";
export const meta = {
    tags: [
        "admin"
    ],
    requireCredential: true,
    requireAdmin: true
};
export const paramDef = {
    type: "object",
    properties: {
        name: {
            type: "string",
            minLength: 1,
            maxLength: 128
        },
        icon: {
            type: "string",
            minLength: 1,
            maxLength: 64
        },
        description: {
            type: "string",
            maxLength: 512,
            default: ""
        }
    },
    required: [
        "name",
        "icon"
    ]
};
export default define(meta, paramDef, async (ps, me)=>{
    const name = ps.name.trim();
    const icon = ps.icon.trim();
    const description = ps.description.trim();
    if (name === "") throw new Error("name is empty");
    if (icon === "") throw new Error("icon is empty");
    const exists = await Plans.findOneBy({
        name
    });
    if (exists) throw new Error("plan name already exists");
    const plan = await Plans.insert({
        id: genId(),
        createdAt: new Date(),
        updatedAt: null,
        name,
        icon,
        description
    }).then((x)=>Plans.findOneByOrFail(x.identifiers[0]));
    insertModerationLog(me, "createPlan", {
        planId: plan.id,
        name
    });
    return await Plans.pack(plan);
});
