import define from "../../../define.js";
import { Plans } from "../../../../../models/index.js";
import { insertModerationLog } from "../../../../../services/insert-moderation-log.js";
export const meta = {
    tags: [
        "admin"
    ],
    requireCredential: true,
    requireAdmin: true
};
export const paramDef = {
    type: "object",
    properties: {
        planId: {
            type: "string",
            format: "misskey:id"
        },
        name: {
            type: "string",
            minLength: 1,
            maxLength: 128
        },
        icon: {
            type: "string",
            minLength: 1,
            maxLength: 64
        },
        description: {
            type: "string",
            maxLength: 512,
            default: ""
        }
    },
    required: [
        "planId",
        "name",
        "icon"
    ]
};
export default define(meta, paramDef, async (ps, me)=>{
    const plan = await Plans.findOneByOrFail({
        id: ps.planId
    });
    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 && exists.id !== plan.id) throw new Error("plan name already exists");
    await Plans.update(plan.id, {
        updatedAt: new Date(),
        name,
        icon,
        description
    });
    insertModerationLog(me, "updatePlan", {
        planId: plan.id,
        name
    });
    return await Plans.pack(plan.id);
});
